-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
99 lines (91 loc) · 2.53 KB
/
server.js
File metadata and controls
99 lines (91 loc) · 2.53 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
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
94
95
96
97
98
99
const http = require("http");
const path = require("path");
const fs = require("fs");
const Mock = require("mockjs");
class MockSever {
/**
* 初始化
* @param {*} port 启用端口
*/
init(port) {
const self = this;
let _port = port || 3000;
let app = http.createServer((request, response) => {
let url = request.url;
let router = self.getRouter();
if (!router[url]) {
// 404
self.handler404Code(response);
} else {
self.getAPIData(request.url)
.then(data => {
self.handler200Code(response, data);
})
.catch(error => {
self.handler500Code(response, error);
});
}
});
app.listen(_port);
console.info(`Mock server is running at http://localhost:${_port}/`);
}
/**
* 获取接口数据
* @param {String} url 接口url
*/
getAPIData(url) {
const self = this;
return new Promise((resolve, reject) => {
try {
let router = self.getRouter();
let filepath = router[url];
let data = JSON.parse(
fs.readFileSync(path.resolve(__dirname, filepath), "utf-8")
);
resolve(Mock.mock(data));
} catch (error) {
reject(error);
}
});
}
/**
* 404
* @param {Object} response
*/
handler404Code(response) {
response.writeHead(404, {
"Content-Type": "application/json; charset=utf-8"
});
response.end("Not Found");
}
/**
*
* @param {Object} response
* @param {String} error 错误信息
*/
handler500Code(response, error) {
response.writeHead(500, {
"Content-Type": "application/json; charset=utf-8"
});
response.end(`Internal Server Error\n${error}`);
}
/**
* 200
* @param {Object} response
* @param {Object} data 返回数据
*/
handler200Code(response, data) {
response.writeHead(200, {
"Content-Type": "application/json; charset=utf-8"
});
response.end(JSON.stringify(data));
}
/**
* 动态获取路由配置
*/
getRouter() {
delete require.cache[require.resolve("./router.conf.js")];
return require("./router.conf.js");
}
}
new MockSever().init();