Getting started

Setup your local via NPM

Installation

Mock JSON Response is distributed via node package manager and can be included in your project using package.json

To add the mock-json-response as a project dependency, create a project

$ mkdir backend-mock
$ cd backend-mock

Once the project is created, initialize npm

$ npm init

follow the instruction on the terminal and enter the necessary information, now it time to install mock-json-response

$ npm install mock-json-response

after the package is loaded in your environment, create 2 new folders for data and functions.

$ mkdir functions
$ mkdir data

Its time to launch the app. Before we launch it we need to import module, create an index.js

/backend-mock/index.js
const logicalDir = __dirname + '/functions';
const dataDir = __dirname + '/data';

require('mock-json-response')(logicalDir, dataDir);

Create a JS file in the functions directory: hello.js

functions/hello.js
module.exports = {
    logic: function ({ headers }, { data }) {
        // logic for data manipulation comes here
    },
    request: {
        method: "GET",
        urlPath: "/mock/test"
    },
    response: {
        status: 200,
        inlineData: {
            message: 'Hello world'
        }
    },
};

Now start the program:

$ node index.js

and wait for the server to start up

Loading routes...
loaded hello.js
Server started

Now send a request via browser, postman or terminal to localhost:3000/mock/test

curl -X GET http://localhost:3000/mock/list

The default port of the server is 3000, once the route is hit, you will get the response

{message:'Hello world'}

You have successfully, setup backend mock.

Last updated