Skip to content

ccbabi/connect-mock-middleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

  1. get /api/xxx
  2. 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, default callback

Notice: you also need to configure your app

app.set('jsonp callback name', 'cb')