-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.js
More file actions
32 lines (29 loc) · 1.02 KB
/
Copy pathrequest.js
File metadata and controls
32 lines (29 loc) · 1.02 KB
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
var fs = require('fs');
var rewrite = require('./rewrite');
const CONTROLLER_PATH = './controller';
const API_FILE_PATH = fs.readdirSync(CONTROLLER_PATH);
function request(app) {
var API = [];
API_FILE_PATH.forEach(function(path){
const fileName = path.substring(path.lastIndexOf("\\") + 1, path.indexOf("."));
projectApi = require(`${CONTROLLER_PATH}/${path}`);
projectApi.forEach(item => {
item.url = `/${fileName + item.url}`;
});
API = API.concat(projectApi);
});
// 遍历接口,返回对应路径的json文件
API.forEach(item => {
if (!item.rewrite) {
app.get(item.url, function (req, res) {
var result = {};
result = require('./mapper' + item.url + '.json');
res.json(JSON.parse(JSON.stringify(result)));
});
} else {
// 自定义接口返回逻辑
rewrite(app, item);
}
});
}
module.exports = request;