-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
32 lines (28 loc) · 928 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
const next = require("next");
const fs = require("fs");
const { createServer } = require("https");
const http = require("http");
const { parse } = require("url");
const dev = false;
const app = next({ dev });
const handle = app.getRequestHandler();
const httpsOptions = {
key: fs.readFileSync("./privkey.pem"),
cert: fs.readFileSync("./fullchain.pem"),
};
const port = 443
app.prepare().then(() => {
// Create HTTP server
http.createServer((req, res) => {
const host = req.headers.host;
res.writeHead(301, { Location: `https://${host}${req.url}` });
res.end();
}).listen(80);
createServer(httpsOptions, (req, res) => {
const parsedUrl = parse(req.url, true);
handle(req, res, parsedUrl);
}).listen(port, (err) => {
if (err) throw err;
console.log(`> Server started on https://localhost:${port}. Mode: ${dev ? "dev" : "prod"}`);
});
});