forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
151 lines (127 loc) · 3.69 KB
/
server.js
File metadata and controls
151 lines (127 loc) · 3.69 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// @ts-check
if (process.env.POLYFILL_FETCH) {
// @ts-expect-error
global.fetch = require('node-fetch').default
// @ts-expect-error
global.Request = require('node-fetch').Request
// @ts-expect-error
global.Headers = require('node-fetch').Headers
}
const { readFileSync } = require('fs')
/** @type {import('next').default} */
// @ts-ignore: missing interopDefault
const next = require('next')
const { join } = require('path')
const { parse } = require('url')
const dev = process.env.NODE_ENV !== 'production'
const dir = __dirname
const port =
(process.env.PORT ? Number.parseInt(process.env.PORT) : undefined) || 3000
const { createServer } = require(
process.env.USE_HTTPS === 'true' ? 'https' : 'http'
)
const app = next({ dev, hostname: 'localhost', port, dir })
const handleNextRequests = app.getRequestHandler()
const httpOptions = {
key: readFileSync(join(__dirname, 'ssh/localhost-key.pem')),
cert: readFileSync(join(__dirname, 'ssh/localhost.pem')),
}
process.on('unhandledRejection', (err) => {
console.error('unhandledRejection:', err)
})
app.prepare().then(() => {
const server = createServer(httpOptions, async (req, res) => {
// let next.js handle assets from /_next/
if (/\/_next\//.test(req.url)) {
return handleNextRequests(req, res)
}
if (req.url === '/no-query') {
return app.render(req, res, '/no-query')
}
if (req.url === '/unhandled-rejection') {
Promise.reject(new Error('unhandled rejection'))
return res.end('ok')
}
if (/setAssetPrefix/.test(req.url)) {
app.setAssetPrefix(`http://127.0.0.1:${port}`)
} else if (/setEmptyAssetPrefix/.test(req.url)) {
app.setAssetPrefix('')
} else {
// This is to support multi-zones support in localhost
// and may be in staging deployments
app.setAssetPrefix('')
}
if (/test-index-hmr/.test(req.url)) {
return app.render(req, res, '/index')
}
if (/dashboard/.test(req.url)) {
return app.render(req, res, '/dashboard')
}
if (/static\/hello\.text/.test(req.url)) {
return app.render(req, res, '/static/hello.text')
}
if (/no-slash/.test(req.url)) {
try {
await app.render(req, res, 'dashboard')
} catch (err) {
res.end(err.message)
}
}
if (/custom-url-with-request-handler/.test(req.url)) {
return handleNextRequests(req, res, parse('/dashboard', true))
}
if (/legacy-methods\/render-to-html/.test(req.url)) {
try {
const html = await app.renderToHTML(req, res, '/dynamic-dashboard', {
q: '1',
})
res.end(html)
} catch (err) {
res.end(err.message)
}
return
}
if (/legacy-methods\/render404/.test(req.url)) {
try {
await app.render404(req, res, parse('/__non_existent__?q=1', true))
} catch (err) {
res.end(err.message)
}
return
}
if (/legacy-methods\/render-error/.test(req.url)) {
try {
res.statusCode = 500
await app.renderError(new Error('kaboom'), req, res, '/dashboard', {
q: '1',
})
} catch (err) {
res.end(err.message)
}
return
}
if (/legacy-methods\/render-error-to-html/.test(req.url)) {
try {
res.statusCode = 500
const html = await app.renderErrorToHTML(
new Error('kaboom'),
req,
res,
'/dashboard',
{ q: '1' }
)
res.end(html)
} catch (err) {
res.end(err.message)
}
return
}
handleNextRequests(req, res)
})
server.listen(port, (err) => {
if (err) {
throw err
}
console.log(`> Ready on http://localhost:${port}`)
})
})