-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
30 lines (27 loc) · 907 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
const next = require("next");
const http = require("http");
const url = require("url");
const path = require("path");
const port = process.env.PORT || 3000;
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
http
.createServer((req, resp) => {
// Parse request url to get its pathName
const parsedUrl = url.parse(req.url, true);
const { pathname } = parsedUrl;
// if a service worker requested, serve it as static file
if (pathname === "/service-worker.js") {
const filePath = path.join(__dirname, ".next", pathname);
app.serveStatic(req, resp, filePath);
} else {
// otherwise let next take care of that
handle(req, resp, parsedUrl);
}
})
.listen(port, () => {
console.log(`Listening on PORT ${port}`);
});
});