-
Notifications
You must be signed in to change notification settings - Fork 0
/
next.server.js
executable file
·77 lines (64 loc) · 1.94 KB
/
next.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* eslint-disable no-console, global-require, import/no-dynamic-require */
const path = require("path");
const glob = require("glob");
const accepts = require("accepts");
const express = require("express");
const nextJS = require("next");
const compression = require("compression");
const jsonServer = require("json-server");
const customRoutes = require("./next.routes");
const dev = process.env.NODE_ENV !== "production";
const port = process.env.PORT || 5000;
const apiURL = process.env.API_URL || "/mock-api";
const app = nextJS({ dev });
const customRoutesHandler = customRoutes.getRequestHandler(app);
const languages = glob
.sync("./src/locales/*.ts")
.map(f => path.basename(f, ".ts"));
app.prepare().then(() => {
const server = express();
if (dev) {
if (process.env.API_DELAY) {
server.use(apiURL, (req, res, next) => {
setTimeout(() => {
next();
}, process.env.API_DELAY);
});
}
server.use(apiURL, jsonServer.rewriter(require("./server/routes")));
server.use(apiURL, jsonServer.router(require("./server/db")));
} else {
server.use(compression());
}
server.use(
"/assets",
express.static(path.join(__dirname, "dist/server/assets"))
);
server.use(
"/appService.js",
express.static(path.join(__dirname, "dist/appService.js"), {
setHeaders: res => {
res.set("Cache-Control", "max-age=0");
}
})
);
server.use((req, res) => {
const accept = accepts(req);
const locale = accept.language(languages);
if (typeof locale === "string") {
req.locale = locale;
} else if (
Array.isArray(locale) &&
locale.filter(item => item !== "*").length > 0
) {
req.locale = locale.filter(item => item !== "*")[0];
} else {
req.locale = "en-NZ";
}
customRoutesHandler(req, res);
});
server.listen(port, err => {
if (err) throw err;
console.log(`> Ready on http://localhost:${port}`);
});
});