-
Notifications
You must be signed in to change notification settings - Fork 2
/
mockhttp.js
34 lines (34 loc) · 970 Bytes
/
mockhttp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module.exports = {
defaultDataFile: "index.js",
baseDist: "/src/data",
port: 8090,
openBrowser: false,
/**
* 中间件,拦截请求之类的操作
* req: express请求参数, res:express返回参数, next:express传递, server: 当前数据操作的server对象
* function(req, res, next, server)
*/
middleWare: {
api: "/setArticle",
callback: function(req, res, next, server) {
console.log("中间件,劫持请求 /setArticle");
let data = req.body;
server
.loadData("getArticles")
.then(result => {
data.ID = result.articles.length + 1;
data.like = 0;
data.comment = 0;
data.thank = false;
data.collection = false;
result.articles.unshift(data);
server.updateData("getArticles", result);
res.send("ok");
})
.catch(err => {
console.log(err);
next();
});
}
}
};