forked from immoren/hellotoall
-
Notifications
You must be signed in to change notification settings - Fork 22
/
server.js
40 lines (34 loc) · 1.11 KB
/
server.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
#!/usr/bin/env node
var http = require("http"),
url = require("url"),
ejs = require("ejs"),
fs = require("fs"),
os = require("os"),
staticResource = require("static-resource"),
port = 8080,
serverUrl,
handler,
favicon;
serverUrl = "http://localhost:" + port + "/";
handler = staticResource.createHandler(fs.realpathSync("./public"));
favicon = fs.realpathSync('./public/favicon.ico');
http.createServer(function (req, res) {
var path = url.parse(req.url).pathname;
if (path === "/") {
res.writeHead(200, {"Content-Type": "text/html"});
res.write(ejs.render(fs.readFileSync("./index.ejs", "utf8"), {
hostname: os.hostname()
}));
res.end();
} else if (req.method === 'GET' && path === '/favicon.ico') {
res.setHeader('Content-Type', 'image/x-icon');
fs.createReadStream(favicon).pipe(res);
} else {
if (!handler.handle(path, req, res)) {
res.writeHead(404);
res.write("404");
res.end();
}
}
}).listen(port);
console.log("The HTTP server has started at: " + serverUrl);