-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathserver.mjs
More file actions
60 lines (53 loc) · 1.49 KB
/
server.mjs
File metadata and controls
60 lines (53 loc) · 1.49 KB
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
55
56
57
58
59
60
/**
* https://nextjs.org/docs/pages/building-your-application/configuring/custom-server
*/
import { createServer } from 'node:http'
import next from 'next'
import { launchEditorMiddleware } from '@react-dev-inspector/middleware'
const dev = process.env.NODE_ENV !== 'production'
const hostname = process.env.HOST || 'localhost'
const port = process.env.PORT ? Number(process.env.PORT) : 3000
const app = next({
dev,
hostname,
port,
})
const handle = app.getRequestHandler()
app.prepare().then(() => {
createServer((req, res) => {
/**
* middlewares, from top to bottom
*/
const middlewares = [
/**
* `react-dev-inspector` server config for nextjs
*
* That's CANNOT be Next.js middleware due to [middleware is in Edge Runtime](https://github.com/vercel/next.js/discussions/34179)
*/
launchEditorMiddleware,
/** Next.js default app handler as middleware */
(req, res) => handle(req, res),
]
const middlewarePipeline = middlewares.reduceRight(
(next, middleware) => (
() => { middleware(req, res, next) }
),
() => {},
)
try {
middlewarePipeline()
}
catch (err) {
console.error('Error occurred handling', req.url, err)
res.statusCode = 500
res.end('internal server error')
}
})
.once('error', (err) => {
console.error(err)
process.exit(1)
})
.listen(port, () => {
console.debug(`\n > Ready on http://${hostname}:${port} \n`)
})
})