-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
65 lines (51 loc) · 1.56 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const next = require('next')
const express = require('express')
const cookieParser = require('cookie-parser')
const dev = process.env.NODE_ENV !== 'production'
const port = process.env.PORT || 4004
const server = express()
const app = next({ dir: '.', dev })
const handler = app.getRequestHandler()
app.prepare()
.then(() => {
server.use(cookieParser())
// Handling login
server.use((req, res, next) => {
if (!req.query.loginToken) return next()
res.cookie('loginToken', req.query.loginToken, {
expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 30),
httpOnly: false
})
return res.redirect(req._parsedUrl.pathname)
})
// Handling logout
server.use((req, res, next) => {
if (!req.query.logout) return next()
res.cookie('loginToken', null, {
expires: new Date(Date.now() - 1000),
httpOnly: false
})
return res.redirect(req._parsedUrl.pathname)
})
server.get('/:course/:lesson/:step?', (req, res) => {
const { course, lesson, step } = req.params
if (course === '_webpack' || course === '_next') {
return handler(req, res, req._parsedUrl)
}
app.render(req, res, '/content', { course, lesson, step })
})
server.use((req, res) => {
handler(req, res)
})
server.listen(port, (err) => {
if (err) {
console.error(err.stack)
process.exit(1)
}
console.log(`App started on port: http://localhost:${port}`)
})
})
.catch((ex) => {
console.error(ex.stack)
process.exit(1)
})