
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
mocker-server-cli
Advanced tools
npm install nodemon -g
npm install mocker-server-cli -g
mocker-server-cli
mocker-server-cli --port 8899 --docs
mockServer.js文件中,
如果是:const mockServer = new MockServer({ dir: ${join(__dirname)}/mock })
则,在/mock 文件夹下,新建任意以.js结尾的文件,系统将自动读取这些文件,文件内容为:
module.exports = {
'get /demo': {
status: 0,
message: '退出',
data: {}
}
}
批量定义请用“,”隔开,如:"get /api/test,get /api/test2,get /api/test3" 以下将会成功模拟出三个接口!!!
module.exports = {
"get /api/test,get /api/test2,get /api/test3": {
status: 0,
message: '退出',
data: {}
}
}
module.exports = {
"get /dome": {
status: 0,
message: '退出',
data: {}
},
//或
"get /dome2": ()=>{
return {
status: 0,
message: '退出',
data: {}
}
}
}
以下代码,接口将3秒后返回结果
module.exports = {
"get /dome": {
sleep: 3000,
status: 0,
message: '退出',
data: {}
}
}
module.exports = {
'get /demo': {
//支持额外的数据过滤函数,有三个参数[req:请求体,res:响应体,data:对应的对象]
filter({ originalUrl = '',...q }, res, resData) {
if (originalUrl.includes('/2')) {
//返回数据为:id===2的数组
return {
...resData,
data:resData.data.filter( item => item.id===2 )
}
}
return resData
},
status: 0,
message: '',
data: {}
},
'get /demo2': {
//filter或handle 可以是异步函数
async filter({ originalUrl = '',...q }, res, resData) {
let sleep=()=>new Promise(resolve => {
setTimeout(()=>{
resolve()
},3000)
})
await sleep()
if (originalUrl.includes('/2')) {
//返回数据为:id===2的数组
return {
...resData,
data:resData.data.filter( item => item.id===2 )
}
}
return resData
},
status: 0,
message: '',
data: {}
},
}
更多详情 mockjs官网
module.exports = {
'get /demo': {
status: 0,
message: '',
data: {
"id":"@id", //随机id
"role|1":["admin","user"], //数组中随机一个值
"userLisr|10":[{
"id|+1":1 // 累加
}] //随机10条数据
}
}
}
注意:能自动识别常用的6种格式:
如果超出以上范围:请在请求request.query添加routerRule,告诉系统将匹配的键,例如: 例如:http://127.0.0.1:8022/demo/a/b?routerRule=/demo/:id/:name 或者:http://127.0.0.1:8022/demo/a/b?routerRule=/demo/*/* 或者:http://127.0.0.1:8022/demo/a/b?routerRule=/demo/${id}/${name}
module.exports = {
//自动匹配
'get /demo/*/*': {
status: 0,
message: '',
data: {
"id":"@id", //随机id
}
},
// 带:的路由,无法识别,需要通过request.query.routerRule='get_demo_:id_:name',告诉系统
// 发出请,应该为:api.get('/demo/11/likuan?routerRule=get_demo_:id_:name')
'get /demo/:id/:name': {
status: 0,
message: '',
data: {
"id":"@id", //随机id
}
}
}
module.exports = {
'get /sansi/api/getuserinfo': {
//原本返回的数据
status: 0,
message: '',
// 现在想代理到另一台机器上的同一路径的资源,只要加上proxy:'127.0.0.1:3436'
// 如果代理,再访问此地址:127.0.0.1:8022/db/get/sansi/api/getuserinfo,
// 将呈现 127.0.0.1:3436/sansi/api/getuserinfo 的数据
proxy:'127.0.0.1:3436',
},
}
祝你使用愉快!有任何问题请联系我!
FAQs
mockServer
We found that mocker-server-cli demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.