forked from rauchg/blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
63 lines (54 loc) · 1.62 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const { createServer } = require("http");
const { parse } = require("url");
const next = require("next");
const atom = require("./lib/atom");
const path = require("path");
const gpgKey = require("fs").readFileSync(path.join(__dirname, "gpg.asc"));
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev, dir: __dirname });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const srv = createServer((req, res) => {
const { pathname } = parse(req.url);
if (/^\/slackin\/?$/.test(pathname)) {
res.writeHead(302, {
Location: "https://github.com/rauchg/slackin"
});
res.end();
return;
}
if (/^\/\d{4}\/.+\/$/.test(pathname)) {
// wordpress used to link to posts with a
// trailing slash, that would 404 in next
// so we redirect them to without
res.writeHead(301, {
Location: pathname.substr(0, pathname.length - 1)
});
res.end();
return;
}
if ("/gpg.asc" === pathname) {
const body = gpgKey;
res.setHeader("Content-Type", "text/plain");
res.setHeader("Content-Length", Buffer.byteLength(body));
res.end(body);
return;
}
if (/^\/atom\/?$/.test(pathname)) {
const body = atom();
res.setHeader("Content-Type", "text/xml");
res.setHeader("Content-Length", Buffer.byteLength(body));
res.end(body);
return;
}
handle(req, res);
});
srv.listen(3000, err => {
if (err) throw err;
console.log("> Ready on http://localhost:3000");
});
process.on("SIGTERM", () => {
console.log("> Shutting down");
srv.close();
});
});