ccs-mock-server
v1.0.6
Published
一、在项目中新建目录,如:mockServer 二、进入mockServer目录,新建package.json ```json { "name": "mock-server", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "api": "nodemon ./mockServer --port=8022 --watch ./mock" }, "auth
Downloads
2
Readme
超级实用的MOCK server 服务
安装步骤
一、在项目中新建目录,如:mockServer 二、进入mockServer目录,新建package.json
{
"name": "mock-server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"api": "nodemon ./mockServer --port=8022 --watch ./mock"
},
"author": "LK",
"license": "ISC",
"devDependencies": {
"nodemon": "^2.0.7",
"@sansi/mockServer": "^1.0.5"
}
}
三、在该目录下打开终端,用npm或yarn 安装依赖
npm install
//或
yarn
四、新建初始配置文件,mockServer.js
const { port } = require('yargs').argv
const MockServer = require('@sansi/mockServer')
const { join } = require('path')
const mockServer = new MockServer({
dir: `${join(__dirname)}/mock`,
defaultData: {}
})
mockServer.server.listen(port, () => console.log(`API服务器:localhost:${port}`))
五、新建mock文件夹,再新建mock数据文件,如:temp.js
module.exports = {
//支持多个地址,返回相同结果,用","隔开,
'get /layout,put /layout/sd,post /layout': {
status: 0,
message: '退出',
data: {}
},
//支持函数,参数url,为访问的路径地址,注意:该地址携带请求方法
'get /layout/ee': url => {
console.log('k', url)
return {
status: 0,
message: 'gf',
data: {}
}
},
//支持额外的数据过滤函数,有三个参数[req:请求体,res:响应体,data:对应的对象]
'get /userInfo,get /userInfo/2': {
filter({ originalUrl ='' },res,data){
if(originalUrl.includes('/2')){
return {
...data,
data:null
}
}
return data
},
status: 0,
message: '获取用户信息',
data: {
userId: 1,
username: 'likuan',
role: 'admin',
roleName: '管理员',
}
}
}
六、终端运行,脚本,访问你看到的地址
npm run api
//或
yarn run api