0.1.1 • Published 7 years ago

connect-mock-middleware v0.1.1

Weekly downloads
5
License
MIT
Repository
github
Last release
7 years ago

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')
0.1.1

7 years ago

0.1.0

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago