connect-mock-middleware
v0.1.1
Published
Very easy to use mock middleware
Downloads
95
Readme
connect-mock-middleware
Very easy to use mock middleware
- Support mockjs syntax
- Support json、jsonp
- Modify the mock data and do not need to restart
Install
npm install connect-mock-middleware
Usage
1. add middleware
const express = require('express')
const path = require('path')
const connectMockMiddleware = require('connect-mock-middleware')
const app = express()
app.use(connectMockMiddleware(path.join(__dirname, 'mock')))
app.listen(3000)
2. write mock file
Suppose I have two requests
get /api/xxx
post /api/<id>/123
<id>
link express router/api/:id/123
, it means that the value changes
The file structure is as follows
mock
└─get
├─api_xxx.js
post
└─api_@id_xxx.js
example: api_xxx.js
module.exports = function ({params, query, body}) {
// params, the path parameter
// query, get query parameter
// body, post submit parameter
return {
code: 1,
data: back,
msg: ''
}
}
Config
- prefix
<string|string[]|function>
: Intercepting API prefixes, default/*
on behalf of all - callback
<string>
: jsonp callback name, defaultcallback
Notice: you also need to configure your app
app.set('jsonp callback name', 'cb')