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

名前追加 #1464

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions form.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ <h1>どちらが食べたいですか?</h1>
<input type="radio" name="yaki-shabu" value="焼き肉" /> 焼き肉
<input type="radio" name="yaki-shabu" value="しゃぶしゃぶ" /> しゃぶしゃぶ
<button type="submit">投稿</button>
<input type="text" name="name" />
</form>
</body>

Expand Down
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);
});