forked from progedu/intro-curriculum-3014
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (39 loc) · 1.1 KB
/
index.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
'use strict';
const http = require('http');
const server = http.createServer((req, res) => {
const now = new Date();
console.info('[' + now + '] Requested by ' + req.connection.remoteAddress);
res.writeHead(200, {
'Content-Type': 'text/html; charset=utf-8'
});
switch (req.method) {
case 'GET':
const fs = require('fs');
const rs = fs.createReadStream('./form.html');
rs.pipe(res);
break;
case 'POST':
let body = [];
req.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
const decoded = decodeURIComponent(body);
console.info('[' + now + '] 投稿: ' + decoded);
res.write('<!DOCTYPE html><html lang="ja"><body><h1>' +
decoded + 'が投稿されました</h1></body></html>');
res.end();
});
break;
default:
break;
}
}).on('error', (e) => {
console.error('[' + new Date() + '] Server Error', e);
}).on('clientError', (e) => {
console.error('[' + new Date() + '] Client Error', e);
});
const port = 8000;
server.listen(port, () => {
console.info('[' + new Date() + '] Listening on ' + port);
});