
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
#back-mock
这个一个模拟后端api数据交互的模块。
##安装
npm install back-mock
##使用
新建一个文件(假如命名为mock.js)。在里面写入如下代码。
var mockServer = require("back-mock");
var path = require("path");
mockServer('0.0.0.0', 2000, path.resolve('./mocks'));
然后在mock.js的同目录新建一个mocks目录,在mocks目录里新建一个文件api.js。api.js里的内容是如下
exports.api_test = {
path: '/api/test',
POST: function() {
return {
xxoo: "this is post"
};
},
GET: function() {
return {
xxoo: "this is get"
}
}
};
exports.api_comptest = {
path: '/api/love',
GET: function(req){
if (req.query.name == "girl"){
return "yes"
}
else {
return "no"
}
}
}
GET和POST方法里的req是express里的req,具体使用可以参考express。
现在就是现在,运行一下mock.js。
node mock.js
就可以访问
查看结果。
http://127.0.0.1/api/love?name=girl
{"data":"yes","status":"ok","description":"no error"}
mocks目录里可以放很多.js文件,只要格式和api.js文件一样就行,还要保证各个文件的path参数不一样。
##tips 启动mock.js时,推荐使用supervior来启动,这样修改文件后不用重启服务,supervior会自动重新启动。
FAQs
Used to simulate the backend API interface
The npm package back-mock receives a total of 1 weekly downloads. As such, back-mock popularity was classified as not popular.
We found that back-mock 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.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.