Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
mock-middleware
Advanced tools
webpack 工程处理 mock 数据中间件 介绍
mock 目录建立与服务地址对应,如/api/user -> mock/api/user.js
# install
npm i -D webpack-mock-middleware
# use
require('webpack-mock-middleware')
webpack before
// mock.js
const isMock = process.env.npm_config_mock;
const eMock = require('./e-mock-middle.js');
module.exports = app => {
if (isMock) {
app.all("/api/*", function(req, res, next) {
eMock(req, res, next);
});
}
};
// vue.config.js devServer
const mockProxy = require('mock.js');
{
devServer: {
before: mockProxy,
}
}
vue cli3 构建项目 下面是工程目录
|--mock
|--mock.js
|--api
|--user.js # /api/user
|--queryuser.js # /api/queryuser
|--vue.config.js
// api/user.js
module.exports = {
data: [
{
username: 'Eric a',
age: 10000
},
{
username: 'Eric b',
age: 10000
},
{
username: 'Eric c',
age: 10000
}
],
code: 200,
message: '操作成功'
};
// api/queryuser.js
module.exports = req => {
// TODO 此处可以通过传参,动态组织返回数据
// 此处有三个参数req,res,next, 具体查看epress文档
let query = req.query;
// 此处可以接收参数
console.log(query);
let data = [
{
id: 1,
name: 'Eric a'
},
{
id: 2,
name: 'Eric b'
}
];
let filterRes = data.filter(itm => itm.id == query.id);
return {
code: 200,
data: filterRes,
message: filterRes.length ? '获取成功' : '暂无数据'
};
};
# package.json中的script加入mock启动标识
{
"scripts": {
"serve": "vue-cli-service serve",
"dev": "npm run serve",
"dev:mock": "npm run dev --mock"
}
}
# 启动
npm run dev:mock
# 判断是否已mock模式启动可通过此获取
process.env.npm_config_mock
// mock.js
const isMock = process.env.npm_config_mock;
const eMock = require('webpack-mock-middleware')
module.exports = app => {
if (isMock) {
app.all("/api/*", function(req, res, next) {
eMock(req, res, next);
});
}
};
在这里以vue.config.js示例
// vue.config.js
const mockProxy = require('mock.js');
{
devServer: {
before: mockProxy,
}
}
FAQs
webpack中处理mock数据
The npm package mock-middleware receives a total of 2 weekly downloads. As such, mock-middleware popularity was classified as not popular.
We found that mock-middleware 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
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.