-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhttp-consult.js
More file actions
43 lines (38 loc) · 1.18 KB
/
Copy pathhttp-consult.js
File metadata and controls
43 lines (38 loc) · 1.18 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
const http = require('http');
const url = require('url');
const responseData = {
ID: 'zhangsan',
Name: '张三',
RegisterDate: '2020年3月1日',
};
function toHTML(data) {
return `
<ul>
<li><span>账号:</span><span>${data.ID}</span></li>
<li><span>昵称:</span><span>${data.Name}</span></li>
<li><span>注册时间:</span><span>${data.RegisterDate}</span></li>
</ul>
`;
}
const server = http.createServer((req, res) => {
const {pathname} = url.parse(`http://${req.headers.host}${req.url}`);
if(pathname === '/') {
const accept = req.headers.accept;
if(req.method === 'POST' || accept.indexOf('application/json') >= 0) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(responseData));
} else {
res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
res.end(toHTML(responseData));
}
} else {
res.writeHead(404, {'Content-Type': 'text/html'});
res.end('<h1>Not Found</h1>');
}
});
server.on('clientError', (err, socket) => {
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});
server.listen(10080, () => {
console.log('opened server on', server.address());
});