-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (42 loc) · 1.25 KB
/
index.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
let http = require("http");
let fs = require("fs");
let path = require("path");
let util = require("util");
let stat = util.promisify(fs.stat);
let readFile = util.promisify(fs.readFile);
let mimeTypes = require("./mime_types");
module.exports = ({ port, bind, webroot }) => {
return new Promise(resolve => {
http.createServer(async ({ url }, res) => {
let pathname = path.join(webroot, url);
try {
let [data, contentType] = await read(pathname);
res.setHeader("Content-Type", contentType);
res.setHeader("Content-Length", data.length);
res.end(data);
} catch(err) {
res.setHeader("Content-Type", "text/plain; charset=utf-8");
if(err.code === "ENOENT") {
res.statusCode = 404;
res.end(`File ${pathname} not found!`);
} else {
res.statusCode = 500;
res.end(`Error getting the file: ${err}.`);
}
}
}).
on("listening", resolve).
listen(port, bind);
});
};
async function read(pathname) {
let stats = await stat(pathname);
if(stats.isDirectory()) {
pathname = path.join(pathname, "index.html");
}
return [await readFile(pathname), contentTypeFor(pathname)];
}
function contentTypeFor(pathname) {
let ext = path.parse(pathname).ext.toLowerCase();
return mimeTypes[ext] || "text/plain";
}