-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
44 lines (33 loc) · 977 Bytes
/
app.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
35
36
37
38
39
40
41
42
var jsonServer = require('json-server')
var jsonfile = require('jsonfile')
var config = require("./config.js");
var server = jsonServer.create()
var router = jsonServer.router({})
var middlewares = jsonServer.defaults()
// cache cgi-->defaultObj
var cgiObjMap = new Map();
config.forEach(function(item){
// Add custom routes for each cgi
server.get(item.cgi, function (req, res) {
var defaultObj;
if(undefined === cgiObjMap.get(item.cgi)){
defaultObj = jsonfile.readFileSync(item.filepath);
if(true === item.need_cache){
cgiObjMap.set(item.cgi,defaultObj);
}
}else{
defaultObj = cgiObjMap.get(item.cgi);
}
// hookfunc can custom the defaultObj
if(undefined !== item.hookfunc){
item.hookfunc(defaultObj,req);
}
// response json format
res.jsonp(defaultObj)
})
});
server.use(middlewares)
server.use(router)
server.listen(3000, function () {
console.log('simple-mock-api Server is running'+',listen on port 3000')
});