The mock-server is able to serve the contents of two distinct directories:
static
: contains static files and sub-folders that are returned directly and are separated by http request methods (GET
,POST
, ...)dynamic
: contains java script files that are executed
File in the static
directory MAY have a .json
extension.
Files in the dynamic
directory have a .js
extensions.
Considering the following directory structure:
/static/
GET/
files/
test.jpg
README
users/
index.json
234.json
POST/
users/
234.json
/dynamic/
users/
567.js
groups/
index.js
misc/
readme.txt.js
GET /users/
will return the/static/users/index.json
fileGET /files/test.jpg
will return the/static/GET/files/test.jpg
fileGET /files/README
will return the/static/GET/files/README
fileGET /users/234
will return the/static/GET/users/234.json
filePOST /users/234
will return the/static/POST/users/234.json
filePOST /users/567
will execute the/dynamic/users/567.js
scriptGET /users/567
will execute the/dynamic/users/567.js
scriptDELETE /groups
will execute the/dynamic/groups/index.js
scriptGET /misc/readme.txt
will execute the/dynamic/misc/readme.txt.js
script
Scripts must export a handle
function that takes two parameters, a request and a response parameter.
exports.handle = function (req, res) {
var password = req.body;
if (password !== 'secret') {
res.status(403).send('wrong password');
} else {
res.send({result: 'ok'});
}
};
yarn add cb-mock-server
In package.json
add e.g.
"scripts": {
"mock:serve": "cb-mock-server --dynamic=mocks/dynamic --static=mocks/static --port=3000"
}
The mock server accepts the following parameters:
--dynamic=
: relative path to directory with dynamic scripts--static=
: relative path to directory with static files--port=
: server port (default is3000
)
If a script is changed (and was already used by the server), the server must be restarted. This can be automated with nodemon
.
First install nodemon
:
yarn add nodemon
Then change the start script in package.json
to e.g.:
"mock:serve": "nodemon --watch mocks/dynamic --exec cb-mock-server --dynamic=mocks/dynamic --static=mocks/static --port=3000"
Create a .js script and specify the response code.