-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
46 lines (38 loc) · 1.29 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')
const pathMatch = require('path-match')
const dev = process.env.NODE_ENV !== 'production'
const port = parseInt(process.env.PORT, 10) || 3000
const app = next({ dev })
const handle = app.getRequestHandler()
const customRoutes = require('./custom-routes')
// Custom route definitions
Object.keys(customRoutes).forEach(route => {
let info = customRoutes[route]
info.match = pathMatch()(route)
})
app.prepare().then(() => {
createServer((req, res) => {
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl
// Match the pathname against the route definitions
const matchingRoute = Object.keys(customRoutes).find(route => {
const info = customRoutes[route]
info.params = info.match(pathname)
return info.params !== false
})
// Either use the matching route or handle it as a regular route
if (matchingRoute !== undefined) {
const info = customRoutes[matchingRoute]
const params = Object.assign(info.params, query)
app.render(req, res, `/${info.page}`, params)
} else {
handle(req, res)
return
}
}).listen(port, err => {
if (err) throw err
console.log('> Ready on http://localhost:3000')
})
})