forked from flipenjor/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
49 lines (40 loc) · 1.22 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
import express from 'express'
import next from 'next'
import bodyparser from 'body-parser'
import cookieparser from 'cookie-parser'
import cors from 'cors'
import { connectSync, isEnvSetup } from './api/connect'
import { isUserSetup } from './api/userMiddleware'
import api from './api'
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev, dir: '.' })
const handle = app.getRequestHandler()
connectSync()
app
.prepare()
.then(() => {
const server = express()
server.use(cors())
server.use(cookieparser())
server.use(bodyparser.json())
server.disable('x-powered-by')
server.disable('etag')
server.use('/api', api)
server.get('*', async (req, res) => {
if (!isEnvSetup()) {
return app.render(req, res, '/env', Object.assign({}, req.query, req.params))
}
if (!await isUserSetup()) {
return app.render(req, res, '/setup', Object.assign({}, req.query, req.params))
}
return handle(req, res)
})
server.listen(process.env.PORT, err => {
if (err) {
throw err
}
console.log('NODE_ENV:', process.env.NODE_ENV)
console.log('ready on port:', process.env.PORT)
})
})
.catch(err => console.error(err))