generated from github-education-resources/primer-copilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment.js
58 lines (55 loc) · 1.48 KB
/
comment.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Create web server
var http = require('http');
var url = require('url');
var querystring = require('querystring');
var fs = require('fs');
var path = require('path');
var comments = [];
http.createServer(function(req, res) {
//console.log(req.url);
var url_parts = url.parse(req.url);
//console.log(url_parts);
//console.log(url_parts.pathname);
var pathname = url_parts.pathname;
if (pathname == '/') {
//console.log('root');
display_form(res);
} else if (pathname == '/comment') {
//console.log('comment');
save_comment(req, res);
} else {
//console.log('other');
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('Page not found');
}
}).listen(8080);
function display_form(res) {
res.writeHead(200, {'Content-Type': 'text/html'});
fs.readFile('comment.html', function(err, data) {
res.end(data);
});
}
function save_comment(req, res) {
var body = '';
req.on('data', function(data) {
body += data;
});
req.on('end', function() {
var post = querystring.parse(body);
comments.push(post.comment);
console.log(comments);
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<html><head><title>Comment</title></head><body>');
res.write('<h1>Comments</h1>');
res.write('<ul>');
comments.forEach(function(comment) {
res.write('<li>' + comment + '</li>');
});
res.write('</ul>');
res.write('<a href="/">Back</a>');
res.write('</body></html>');
res.end();
});
}
console.log('Server running at http://localhost:8080/');
// End of comment.js