-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
40 lines (33 loc) · 930 Bytes
/
server.js
File metadata and controls
40 lines (33 loc) · 930 Bytes
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
/**
* @file Entry point for the custom Express server used with Next.js.
* This server forwards all requests to Next.js for handling.
*/
import express from "express";
import next from "next";
import { parse } from "url";
/**
* Initialize Next.js application.
* @type {import('next').NextServer}
*/
const app = next({ dev: false });
/**
* Request handler provided by Next.js.
*/
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = express();
/**
* Handle all incoming requests using Next.js' handler.
* @param {import('express').Request} req
* @param {import('express').Response} res
*/
server.all("*", (req, res) => {
const parsedUrl = parse(req.url, true);
handle(req, res, parsedUrl);
});
/** @type {number|string} */
const port = process.env.PORT || 8080;
server.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
});