1.0.13 • Published 5 years ago
@colamda/mock-server v1.0.13
Colamda mock-server
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.jsGET /users/will return the/static/users/index.jsonfileGET /files/test.jpgwill return the/static/GET/files/test.jpgfileGET /files/READMEwill return the/static/GET/files/READMEfileGET /users/234will return the/static/GET/users/234.jsonfilePOST /users/234will return the/static/POST/users/234.jsonfilePOST /users/567will execute the/dynamic/users/567.jsscriptGET /users/567will execute the/dynamic/users/567.jsscriptDELETE /groupswill execute the/dynamic/groups/index.jsscriptGET /misc/readme.txtwill execute the/dynamic/misc/readme.txt.jsscript
Example scripts
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'});
}
};Install and setup
yarn add @colamda/mock-serverIn package.json add e.g.
"scripts": {
"mock:serve": "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)
Watch & Reload
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 nodemonThen change the start script in package.json to e.g.:
"mock:serve": "nodemon --watch mocks/dynamic --exec mock-server --dynamic=mocks/dynamic --static=mocks/static --port=3000"Use cases
Return a different http code than 200
Create a .js script and specify the response code.