-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (49 loc) · 1.31 KB
/
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
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
const path = require('path')
const next = require('next')
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const withSass = require('@zeit/next-sass')
const sqlModel = require('./server/models')
const routes = require('./server/routes')
const conf = withSass({
sassLoaderOptions: {
includePaths: [path.resolve('./client/styles')]
}
})
const port = process.env.PORT || 3000
const dev = process.env.NODE_ENV !== 'production'
const app = next({
dev,
conf,
dir: './client'
})
const handle = app.getRequestHandler();
(async () => {
try {
await sqlModel.sequelize.sync()
await app.prepare()
const server = express()
server.use(cors())
server.use(bodyParser.json())
server.use(bodyParser.urlencoded({ extended: false }))
server.use('/api/v1', routes)
server.get('*', (req, res) => {
return handle(req, res)
})
// error handler
server.use((err, req, res, next) => {
if (dev) {
return res.status(err.status || 500).json({ message: err.message, stack: err.stack })
}
return res.status(500)
})
server.listen(port, (err) => {
if (err) throw err
console.log('> Ready on http://localhost:3000')
})
} catch (err) {
console.error(err.stack)
process.exit(1)
}
})()