forked from abinnz/jdlite-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
40 lines (37 loc) · 1.28 KB
/
main.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
const faker = require('./MovementFaker.js');
const http = require('http');
const { URL } = require('url');
const port = 5889;
http.createServer(function (request, response) {
try {
const { host } = request.headers;
const parseUrl = new URL(request.url, `http://${host}`);
const url = parseUrl.pathname;
if (url == '/log') {
let body = faker.getBody();
response.writeHead(200, {
'Content-Type': 'application/json'
});
body.then(item => {
response.end(JSON.stringify(item));
});
} else if (url == '/batchLog') {
let count = parseInt(parseUrl.searchParams.get('count') || 1);
let bodyArray = faker.getBodyArray(count);
response.writeHead(200, {
'Content-Type': 'application/json'
});
Promise.all(bodyArray).then(values => {
response.end(JSON.stringify(values));
});
} else {
response.writeHead(200, {
'Content-Type': 'text/plain'
});
response.end('hello world');
}
} catch (err) {
console.error(err);
}
}).listen(port);
console.log('Server running at http://127.0.0.1:' + port + '/');