mock-server
Node mock server based on Koa.
- Writing routes configuration.
- Custom request handlers.
- Watching file changes, and restarting server.
Install
npm install @airquality/mock-server
Usage
All .js
files in cwd(current work directory) will be watched and treated as routes configuration files. Every .js
file must be exported an array property routes
. Every route object in routes
must exist three properties: path
, method
and handler
. The handler
function will be injected a request data object. See following:
module.exports = {
routes: [
{
path: '/',
method: 'get',
handler: ({ headers, params, query, body, cookies, ctx }) => {
return 'Hello World!'
},
},
],
}
In your package.json
file, add a script to start mock server by use mock-server
command.
...
script: {
"mock": "mock-server",
...
}
...
Finally, type npm run mock
in your work directory.
npm run mock
Options
Set mockServerOptions
in package.json
file to change default options.
...
"mockServerOptions": {
"path": "./routes",
"port": 4001
}
...
Simulated Data
You can use faker.js to generate data.
License
MIT.