-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal-api-server.js
131 lines (115 loc) · 3.14 KB
/
local-api-server.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const express = require('express'),
readline = require('readline'),
rl = readline.createInterface(process.stdin),
http = require('http'),
app = express(),
server = http.createServer(app),
bodyParser = require('body-parser');
const debugDataInfo = [
{
dataName: 'getTestData',
fileName: 'test'
},
{
dataName: 'test1',
fileName: 'test1'
},
{
dataName: 'test2',
fileName: 'test2'
}
];
const allFileName = debugDataInfo.map(info => info.fileName);
var data = {};
function initDebugData() {
console.log('deleting require cache...');
allFileName.forEach(
fileName =>
delete require.cache[require.resolve(`./data/${fileName}.json`)]
);
}
function loadDebugData() {
initDebugData();
debugDataInfo.forEach(dataInfo => {
data[dataInfo.dataName] = require(`./data/${dataInfo.fileName}.json`);
});
console.log(data);
}
function ok(something) {
return {
code: 2000,
data: something || null,
message: 'OK'
};
}
function error(message, code, data) {
return {
code: code || 20001,
data: data || null,
message: message
};
}
app.use(bodyParser.json());
app.get('/api/download', req => {
console.log(req.url);
// res.sendFile(path.join(__dirname, './data/download.sample.zip'));
});
app.all('*', (req, res, next) => {
let date = new Date();
console.log(date.toLocaleString());
console.log(`Request-URL(No Params): ${req.url.split('?')[0]}`);
console.log('Request-Params:', req.query);
console.log(`Request-Method: ${req.method}`);
console.log(`X-Feature-Path: ${req.header('X-Feature-Path')}`);
console.log(
'------------------------------------------------------------------------------'
);
return next();
});
app.all('*', async (req, res, next) => {
const url = req.url.split('?')[0],
method = req.method,
params = req.query; // GET POST PUT DELETE...
switch (url) {
case '/getTestData': {
console.log(method);
if (method === 'GET') {
return res.json(ok(data.getTestData)).end();
} else {
return res
.json(
error(
'Method is not right! The method of this API should be GET!'
)
)
.end();
}
}
case '/test1': {
return res.json(ok(data.test1)).end();
}
case '/test2': {
return res.json(ok(data.test2)).end();
}
default:
return next();
}
});
function start() {
loadDebugData();
server.listen(8899, () => {
console.log(`api @ 8899`);
console.log(`press enter to reset data to initial state`);
console.log(
'------------------------------------------------------------------------------'
);
});
}
rl.addListener('line', () => {
console.log(`closing...`);
server.close(() => {
console.log(`restarting...`);
start();
});
});
start();