forked from hzsrc/dynamic-mocker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbyMock.js
94 lines (84 loc) · 3.17 KB
/
byMock.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var path = require('path')
var fs = require('fs')
var url = require('url')
var querystring = require('querystring')
var mockByData = require('./mockByData.js')
function byMock(config, req, res, next) {
if (config.mockEnabled) {
var urlPart = url.parse(req.url);
req.query = querystring.parse(urlPart.query) //暂存备用
var pathname = urlPart.pathname;
if (config.mapFile) {
pathname = config.mapFile(pathname, req)
}
if (!config.checkPath || config.checkPath(pathname)) {
var paths = [].concat(config.mockPath);
var i = -1;
var byNextPath = function () {
i++;
if (i < paths.length) {
var mockFile = path.join(config.relativePath, paths[i], pathname + '.js');
if (fs.existsSync(mockFile)) {
//模拟数据,从mock文件夹获取
mockByFile(config, req, res, mockFile, byNextPath);
} else {
// 像`/api/delete/[id]`,这样的动态url走 `__DEFAULT.js`,设置query.ThisUrlPart=[id]
mockFile = path.join(config.relativePath, paths[i], path.dirname(pathname), '__DEFAULT.js');
if (fs.existsSync(mockFile)) {
req.query.ThisUrlPart = path.basename(pathname)
mockByFile(config, req, res, mockFile, byNextPath);
} else {
byNextPath()
}
}
} else {
//没有mock数据,代理给服务处理
next();
}
};
byNextPath();
}
} else {
next()
}
}
//使用js文件模拟内容输出
function mockByFile(config, req, res, mockFile, byNextPath) {
var fullMockFile = path.resolve(config.relativePath, mockFile);
var js = '(function(){var exports={},module={exports:exports};' + fs.readFileSync(fullMockFile) + ';return module.exports})()';
//delete require.cache[fullMockFile]; //根据绝对路径,清空缓存的对象
var responseFn = function (status, headers, body) {
res.writeHead(status, headers);
return res.end(body);
}
try {
//var mockData = require(fullMockFile) || {};
var mockData = eval(js)
req.readReqData = readPost.bind(null, req)
return mockByData(config, mockData, req, responseFn, byNextPath)
} catch (e) {
var error = `Error in file:${mockFile}:\n` + e;
console.error(error);
responseFn(500, {}, JSON.stringify(error));
}
}
function readPost(req, callback) {
if (req.method == 'POST') {
var qBody = '';
req.on('data', function (data) {
qBody += data;
});
req.on('end', function () {
if (qBody) {
try {
qBody = eval('(' + qBody + ')'); //尝试将json字符串转为对象
} catch (e) {
}
}
callback(qBody)
});
} else {
callback({})
}
}
module.exports = byMock