@jarvis-catalyst/mockserver
v1.0.14
Published
simple mock server
Downloads
150
Readme
Jarvis Catalyst Mock Server
Simple mock server without dependencies, zero configuration and super easy ussing
How to use
this library creates a folder service
and static
- install this package
npm i @jarvis-catalyst/mockserver
- create a folder
mockserver
at the root of your project - create into
mockserver
folder aindex.js
file - add this code
const mock = require("@jarvis-catalyst/mockserver");
mock.default({
dir: __dirname,
});
- add the task to your
package.json
"devserver": "node ./mockserver/index.js"
Props
port
: defines the port where the server starts (default: 4000)dir
: define where services are scanneddelayResponse
: defines the delay time when the server responds (default: 100ms)
Create a Endpoint
- create your services into
services
folder - it is recommended to use the following name structure
{verb}-{path}-your-{endpint}.js
Example:
// for a service such that
module.exports = () => ({
path: "/indexes/v1/products/facets",
method: "GET",
response: (req, res, querystring, data, params) => ({
data: {
data: {},
},
status: 200,
}),
delay: 1000,
});
// the file name will be
// get-indexes-v1-products-facets.js
props your endpoints
// get_hello-world.js
const mockAsync = async () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Hello World");
}, 100);
});
};
module.exports = () => ({
path: "/hello-world/:id", // :id is a varaible param
method: "GET", // http verb
/**
* @pamam {IncomingMessage} res
* @param {ServerResponse} res
* @param {any} querystring - object /page?12 > { page: 12}
* @param {any} data - object boby
* @param {string[]} params - array http://foo.com/path/:id/:name > [12, 'jhon']
*/
response: async (req, res, querystring, data, params) => {
const message = await mockAsync();
return {
// data represent a return body
data: {
id: params[0], // is equal to value into :id
message,
},
status: 200, // http status code
};
},
delay: 500, // define delay this endpoint
});