faker-mock
v1.0.9
Published
A simple mock server build with Mock.js and Faker.js
Downloads
9
Readme
FAKER-MOCK
A simple mock server build with Mock.js and Faker.js
Usage
Install
$ npm i faker-mock --save-dev
Example
use middleware with express
import { middleware } from 'faker-mock'
import express from 'express'
const app = express()
const config = [
{
path: '/user/:id',
method: 'get',
delay: 1000,
status: 200,
data: {
count: 10,
'result|10': [
{
id: '@fake(uuid)', // use method of faker.js
month: '@fake(date.month)',
name: '@cname' // or use method of mockjs itself
}
]
}
},
...
]
app.use(middleware(config, 'zh_CN'))
app.listen(3000)
or start a server directly
import { server } from 'faker-mock'
server({
port: 3000,
config
})
use a file path as config
// config.js
module.exports = [
{
path: '/user/:id',
delay: 1000,
data: {
count: 10,
'result|10': [
{
id: '@fake(uuid)',
name: '@fake(findName)'
}
]
}
},
...
]
// server.js
import path from 'path'
const filePath = path.resolve('./config.js')
...
app.use(middleware(filePath))
// or
server({ config: filePath })
send a request
curl 'localhost:3000/user/123'
wait 1000ms ...
output:
{
"count": 10,
"result": [
{
"id": "9560d812-bd00-40af-af57-783a829e4dd1",
"month": "September",
"name": "贾秀英"
},
{
"id": "44871a2d-9f69-4c74-9acf-f1c4ebe27620",
"month": "June",
"name": "江秀英"
},
{
"id": "ae32f880-cd0e-425c-bae3-35b2d577bacb",
"month": "February",
"name": "乔勇"
},
{
"id": "d7853b34-685f-4520-9b80-afdc78ec8a08",
"month": "January",
"name": "赵明"
},
{
"id": "ec81f1d0-36c5-4967-a8b4-2a3c5abe9003",
"month": "October",
"name": "何娜"
},
{
"id": "0cacf13d-ad28-46ab-8342-b769cb031b99",
"month": "August",
"name": "萧杰"
},
{
"id": "c3a1b7fe-f32c-4ac6-ac3a-bd72b2554e9a",
"month": "January",
"name": "赵平"
},
{
"id": "75bcabcd-04ed-4b1f-9804-803f4d6d7fbe",
"month": "January",
"name": "唐娜"
},
{
"id": "16cefe01-7bbc-4972-ac72-e4b54cb60171",
"month": "August",
"name": "孙明"
},
{
"id": "381ec33b-39f6-4a6c-bb51-6ae0fed4f09f",
"month": "October",
"name": "廖勇"
}
]
}