2020-01-29 12:30:50 +01:00
|
|
|
const path = require('path')
|
2020-02-07 16:21:34 +01:00
|
|
|
const { NODE_ENV } = process.env
|
|
|
|
require('dotenv').config({ path: path.join(__dirname, `../.env.${NODE_ENV}`) })
|
2020-01-29 11:54:03 +01:00
|
|
|
const express = require('express')
|
2020-01-29 12:30:50 +01:00
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
const session = require('express-session')
|
2020-01-29 11:54:03 +01:00
|
|
|
const { Nuxt, Builder } = require('nuxt')
|
|
|
|
const config = require('../nuxt.config.js')
|
2020-02-07 10:21:53 +01:00
|
|
|
const sessionsController = require('./controllers/authorize')
|
|
|
|
const contributionController = require('./controllers/contribute')
|
2020-02-06 15:24:04 +01:00
|
|
|
const models = require('./models')
|
2020-01-29 12:30:50 +01:00
|
|
|
|
|
|
|
const app = express()
|
2020-01-29 11:54:03 +01:00
|
|
|
|
|
|
|
async function start() {
|
2020-02-07 16:21:34 +01:00
|
|
|
config.dev = NODE_ENV !== 'production'
|
2020-02-06 15:24:04 +01:00
|
|
|
await models.sequelize.sync()
|
2020-01-29 11:54:03 +01:00
|
|
|
const nuxt = new Nuxt(config)
|
|
|
|
|
|
|
|
const { host, port } = nuxt.options.server
|
|
|
|
|
|
|
|
// Build only in dev mode
|
|
|
|
if (config.dev) {
|
|
|
|
const builder = new Builder(nuxt)
|
|
|
|
await builder.build()
|
|
|
|
} else {
|
|
|
|
await nuxt.ready()
|
|
|
|
}
|
2020-01-29 12:30:50 +01:00
|
|
|
app.use(
|
|
|
|
session({
|
|
|
|
secret: process.env.SESSION_SECRET,
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: true
|
|
|
|
})
|
|
|
|
)
|
|
|
|
app.use((req, res, next) => {
|
|
|
|
res.locals.session = req.session
|
|
|
|
next()
|
|
|
|
})
|
2020-01-29 17:59:37 +01:00
|
|
|
|
2020-01-30 13:15:42 +01:00
|
|
|
app.use(bodyParser.urlencoded({ extended: true }))
|
|
|
|
app.use(bodyParser.json())
|
|
|
|
|
2020-02-08 19:47:21 +01:00
|
|
|
app.use('/api', sessionsController)
|
|
|
|
app.use('/api', contributionController)
|
|
|
|
|
2020-01-29 17:59:37 +01:00
|
|
|
// Give nuxt middleware to express
|
|
|
|
app.use(nuxt.render)
|
2020-01-29 12:30:50 +01:00
|
|
|
|
|
|
|
app.listen(port, host, () => {
|
|
|
|
console.log(`Server is running on port ${port}.`)
|
2020-01-29 11:54:03 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
start()
|