-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
66 lines (55 loc) · 1.63 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
66
const express = require('express')
const path = require('path')
const next = require('next')
const cookieParser = require('cookie-parser')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dir: '.', dev })
const handle = app.getRequestHandler()
app
.prepare()
.then(() => {
const server = express()
server.use(cookieParser())
// Handling login
server.use((req, res, next) => {
if (!req.query.loginToken) return next()
console.log(req.query)
res.cookie('loginToken', req.query.loginToken, {
expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 30),
httpOnly: false
})
if (req.query.next) {
return res.redirect(req.query.next)
}
return res.redirect('/')
})
// 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)
})
// serve service worker
server.get('/sw.js', (req, res) =>
res.sendFile(path.resolve('./.next/sw.js'))
)
server.get('/push-sw.js', (req, res) =>
res.sendFile(path.resolve('./push-sw.js'))
)
// tos
server.get('/tos', (req, res) =>
res.sendFile(path.resolve('./static/tos.html'))
)
server.get('*', (req, res) => handle(req, res))
server.listen(process.env.PORT || 3000, err => {
if (err) throw err
console.log('> App running on port', process.env.PORT || 3000)
})
})
.catch(ex => {
console.error(ex.stack)
process.exit(1)
})