-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
35 lines (31 loc) · 981 Bytes
/
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
const express = require("express");
const next = require("next");
const {
fetchArticle,
fetchSection,
fetchStructure,
fetchCodes
} = require("./src/api");
const routes = require("./src/routes");
const app = next({ dev: process.env.NODE_ENV !== "production" });
const handler = routes.getRequestHandler(app);
const PORT = process.env.PORT || 3000;
app.prepare().then(() => {
express()
// API proxy
.get("/codes.json", async (req, res) => {
res.json(await fetchCodes());
})
.get("/code/:code.json", async (req, res) => {
res.json(await fetchStructure(req.params.code));
})
.get("/code/:code/article/:article.json", async (req, res) => {
res.json(await fetchArticle(req.params.code, req.params.article));
})
.get("/code/:code/section/:section.json", async (req, res) => {
res.json(await fetchSection(req.params.code, req.params.section));
})
// next-routes handler
.use(handler)
.listen(PORT);
});