-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathserver.js
More file actions
31 lines (26 loc) · 834 Bytes
/
server.js
File metadata and controls
31 lines (26 loc) · 834 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
#!/usr/bin/env node
var http = require("http"),
url = require("url"),
ejs = require("ejs"),
fs = require("fs"),
staticResource = require("static-resource"),
port = 8080,
serverUrl,
handler;
serverUrl = "http://localhost:" + port + "/";
handler = staticResource.createHandler(fs.realpathSync("./public"));
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")));
res.end();
} 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);