
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
fe-api-mocker
Advanced tools
$ npm i fe-api-mocker -D
./mockData.js
默认为 GET 请求,如果是 POST 或者其他请求,请以[method api]方式配置, 如:post /api
/*
* 参数类型
* status 服务器返回状态码, 默认:200
* delay 请求延时,默认:100ms
* headers 返回头header
* data 返回数据, 可以为json,或者函数,函数接收req请求对象,可以获取query,body,或者params
*/
module.exports = {
// 请求的方法和api地址。
// 默认为 GET 请求。
"api/1": {
status: 200,
// 自定义header
headers: {
'X-Foo': 'bar'
},
// 请求延时,默认100ms
delay: 1000,
// 返回数据
data: {
msg: 'response data'
}
},
// 以函数定义返回数据
"api/2": {
data(req) {
return {
role: req.query.username === 'paul' ? 'admin': 'not admin'
}
}
},
// 内置mockjs
"api/3": {
data: {
// 定义长度为5~10的list数组
'list|5-10': [{
// 属性 id 是一个自增数,起始值为 1,每次增 1
'id|+1': 1
}]
}
},
// 发送POST请求
"post api/1": {
data: {
msg: 'response data'
}
},
// 以函数定义返回数据
"post api/2": {
data(req) {
return {
role: req.body.username === 'paul' ? 'admin': 'not admin',
name: req.body.username
}
}
},
}
如果使用基于express的server,如vue-cli, webpack-dev-sever, 需要配置devServer.before
module.exports = {
devServer: {
before(app) {
// 假设接口文件是 ./mockData.js
mock(app, path.resolve(__dirname, './mockData.js'));
}
}
}
如果直接本地跑 express,则配置如下
const mock = require('fe-api-mocker');
const app = require('express')();
// 略过其他逻辑……
// 假设接口文件是 ./mockData.js
mock(app, path.resolve(__dirname, './mockData.js'));
app.listen(3000);
// 这里要使用你自己的端口号
fetch('http://localhost:3000/api/2?username=paul')
.then(d => d.json())
.then(d => {
console.log(d)
})
FAQs
A api mocker for fe development.
The npm package fe-api-mocker receives a total of 0 weekly downloads. As such, fe-api-mocker popularity was classified as not popular.
We found that fe-api-mocker 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
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.