2020-01-29 12:30:50 +01:00
|
|
|
require('dotenv').config()
|
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
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 morgan = require('morgan')
|
|
|
|
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-01-29 12:30:50 +01:00
|
|
|
const sessionsController = require('./controllers/sessions')
|
|
|
|
const contributionController = require('./controllers/contributions')
|
|
|
|
|
|
|
|
const app = express()
|
2020-01-29 11:54:03 +01:00
|
|
|
|
|
|
|
async function start() {
|
2020-01-29 12:30:50 +01:00
|
|
|
config.dev = process.env.NODE_ENV !== 'production'
|
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 17:59:37 +01:00
|
|
|
app.use('/api', sessionsController)
|
|
|
|
app.use('/api', contributionController)
|
2020-01-29 11:54:03 +01:00
|
|
|
|
2020-01-29 12:30:50 +01:00
|
|
|
app.use(bodyParser.urlencoded({ extended: true }))
|
|
|
|
app.use(bodyParser.json())
|
|
|
|
|
2020-01-29 17:59:37 +01:00
|
|
|
const accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), {
|
|
|
|
flags: 'a'
|
|
|
|
})
|
2020-01-29 12:30:50 +01:00
|
|
|
app.use(morgan('combined', { stream: accessLogStream }))
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// 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()
|