forked from garann/node-for-frontend-devs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-02.js
More file actions
34 lines (29 loc) · 708 Bytes
/
02-02.js
File metadata and controls
34 lines (29 loc) · 708 Bytes
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
var http = require("http"),
path = require("path"),
fs = require("fs");
http.createServer(function(req, res) {
var filename = path.basename(req.url) || "index.html",
ext = path.extname(filename),
localPath = __dirname + "/public/";
if (ext == ".html") {
localPath += filename;
path.exists(localPath, function(exists) {
if (exists) {
getFile(localPath, res);
} else {
res.writeHead(404);
res.end();
}
});
}
}).listen(8000);
function getFile(localPath, res) {
fs.readFile(localPath, function(err, contents) {
if (!err) {
res.end(contents);
} else {
res.writeHead(500);
res.end();
}
});
}