Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

練習なのでマージ不要 #1440

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 30 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
'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;
}
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 rawData = '';
req.on('data', (chunk) => {
rawData = rawData + chunk;
}).on('end', () => {
const decoded = decodeURIComponent(rawData);
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);
console.error('[' + new Date() + '] Server Error', e);
}).on('clientError', (e) => {
console.error('[' + new Date() + '] Client Error', e);
console.error('[' + new Date() + '] Client Error', e);
});
const port = 8000;
server.listen(port, () => {
console.info('[' + new Date() + '] Listening on ' + port);
});
console.info('[' + new Date() + '] Listening on ' + port);
});