-
Notifications
You must be signed in to change notification settings - Fork 13
/
server.mjs
54 lines (47 loc) · 1.73 KB
/
server.mjs
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
import { createServer } from 'http';
import { parse } from 'url';
import next from 'next';
const dev = process.env.NODE_ENV !== 'production';
const hostname = 'localhost';
const port = Number(process.env.PORT) || 3000;
// when using middleware `hostname` and `port` must be provided below
const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();
// cannot import from next.config.mjs because this will break env load
const CACHE_CONTROL_HEADER = 'x-cache-control';
// allows us to override cache-control header
const overrideSetHeader = (res) => {
const setHeader = res.setHeader;
let cacheControlOverwritten = false;
res.setHeader = function (header, value) {
if (header.toLowerCase() === CACHE_CONTROL_HEADER) {
cacheControlOverwritten = true;
return setHeader.call(this, 'Cache-Control', value);
}
if (header.toLowerCase() === 'cache-control' && cacheControlOverwritten) {
return this;
}
return setHeader.call(this, header, value);
};
};
// eslint-disable-next-line @typescript-eslint/no-floating-promises
app.prepare().then(() => {
const server = createServer(async (req, res) => {
// Be sure to pass `true` as the second argument to `url.parse`.
// This tells it to parse the query portion of the URL.
const parsedUrl = parse(req.url, true);
overrideSetHeader(res);
await handle(req, res, parsedUrl);
})
.once('error', (err) => {
console.error(err);
process.exit(1);
})
.listen(port, () => {
console.debug(`> Ready on http://${hostname}:${port}`);
});
// prevents malicious client from slowly sending headers and rest of request
server.headersTimeout = 10_000;
server.requestTimeout = 30_000;
server.maxHeadersCount = 50;
});