-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy path03-04.js
37 lines (32 loc) · 906 Bytes
/
03-04.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
var http = require("http"),
fs = require("fs"),
querystring = require("querystring");
http.createServer(function(req, res) {
var data = "";
if (req.method == "GET") {
getFile(__dirname + "/public/simpleForm.html", res);
}
if (req.method == "POST") {
req.on("data", function(chunk) {
data += chunk;
});
req.on("end", function() {
var params = querystring.parse(data),
userName = params.firstName + " " + params.lastName,
html = "<!doctype html>" +
"<html><head><title>Hello " + userName + "</title></head>" +
"<body><h1>Hello, " + userName + "!</h1></body></html>";
res.end(html);
});
}
}).listen(8000);
function getFile(localPath, res) {
fs.readFile(localPath, function(err, contents) {
if (!err) {
res.end(contents);
} else {
res.writeHead(500);
res.end();
}
});
}