-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
44 lines (37 loc) · 1.15 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
/**
* Based on https://zenn.dev/sotszk/articles/b4e6a4e19d2e35
*/
const express = require('express');
const next = require('next');
const https = require('https');
const fs = require('fs');
const port = parseInt(process.env.PORT || '443');
const host = '0.0.0.0';
const app = next({
dev: process.env.NODE_ENV !== 'production',
});
const handle = app.getRequestHandler();
(async () => {
await app.prepare();
const expressApp = express();
expressApp.get('*', (req, res) => handle(req, res));
// Use HTTPS if HTTPS option enabled
const hasCertificates =
fs.existsSync('./certificates/localhost.key') &&
fs.existsSync('./certificates/localhost.crt');
const useHttps =
process.env.HTTPS === 'true' &&
hasCertificates;
if (useHttps) {
const options = {
key: fs.readFileSync('./certificates/localhost.key'),
cert: fs.readFileSync('./certificates/localhost.crt'),
};
const server = https.createServer(options, expressApp);
server.listen(port, host);
console.log(`> Ready on https://localhost:${port}`);
} else {
expressApp.listen(port, host);
console.log(`> Ready on http://localhost:${port}`);
}
})();