-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
32 lines (24 loc) · 901 Bytes
/
index.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
const micro = require('micro')
const next = require('next')
const extractSessionFromCookie = require('./modules/extractSessionFromCookie')
/*
* A custom next.js server using micro. This is primarily so we can add some
* middleware; we don't do anything special in terms of routing.
*/
const isProduction = process.env.NODE_ENV === 'production'
const port = 3000
// Let `next` do everything but use this opportunity to parse the cookie,
// decode the JWT and cache it at `req.session`
const main = async () => {
const app = next({ dev: !isProduction })
const nextRequestHandler = app.getRequestHandler()
await app.prepare()
micro((req, res) => {
// Extract the session and cache it
req.session = extractSessionFromCookie(req)
// Let next handle the request
return nextRequestHandler(req, res)
}).listen(port)
console.info(`listening on port ${port}...`)
}
main()