-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.ts
74 lines (58 loc) · 1.66 KB
/
server.ts
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
64
65
66
67
68
69
70
71
72
73
74
import express from "express";
import next from "next";
import { parse } from "url";
import { join } from "path";
import fs from "fs";
import { config } from "./config/server";
import { generateFeed } from "./lib/feed";
const dev = config.NODE_ENV !== "production";
const port = config.PORT;
const app = next({ dev });
const handle = app.getRequestHandler();
const serviceWorkerPath = join(__dirname, ".next", "/service-worker.js");
const serviceWorker = fs.readFileSync(serviceWorkerPath);
app
.prepare()
.then(() => {
const server = express();
server.get("/feed/:type?", async (req, res) => {
const type = req.params.type;
const feed = await generateFeed();
const rss = feed.rss2();
const atom = feed.atom1();
const json = feed.json1();
if (type === "atom") {
res.contentType("application/atom+xml");
res.send(atom);
return;
}
if (type === "json") {
res.contentType("application/json");
res.send(json);
return;
}
res.contentType("application/rss+xml");
res.send(rss);
return;
});
server.get("*", (req, res) => {
const parsedUrl = parse(req.url, true);
const { pathname } = parsedUrl;
if (pathname === "/service-worker.js") {
res.contentType("application/javascript");
res.send(serviceWorker);
return;
}
return handle(req, res);
});
server.post("*", (req, res) => {
return handle(req, res);
});
server.listen(port, () => {
console.log(`> Ready on http://localhost:${port}`);
});
})
.catch((ex) => {
console.error(ex.stack);
process.exit(1);
});