2016-12-04 18:16:10 +00:00
|
|
|
const Nuxt = require('nuxt')
|
|
|
|
const bodyParser = require('body-parser')
|
2016-12-07 16:03:52 +00:00
|
|
|
const session = require('express-session')
|
2016-12-04 18:16:10 +00:00
|
|
|
const app = require('express')()
|
|
|
|
|
|
|
|
// Body parser, to access req.body
|
|
|
|
app.use(bodyParser.json())
|
|
|
|
|
2016-12-07 16:03:52 +00:00
|
|
|
// Sessions to create req.session
|
|
|
|
app.use(session({
|
|
|
|
secret: 'super-secret-key',
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: false,
|
|
|
|
cookie: { maxAge: 60000 }
|
2016-12-04 18:16:10 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
// POST /api/login to log in the user and add him to the req.session.authUser
|
|
|
|
app.post('/api/login', function (req, res) {
|
|
|
|
if (req.body.username === 'demo' && req.body.password === 'demo') {
|
|
|
|
req.session.authUser = { username: 'demo' }
|
|
|
|
return res.json({ username: 'demo' })
|
|
|
|
}
|
|
|
|
res.status(401).json({ error: 'Bad credentials' })
|
|
|
|
})
|
|
|
|
|
2016-12-07 16:03:52 +00:00
|
|
|
// POST /api/logout to log out the user and remove it from the req.session
|
|
|
|
app.post('/api/logout', function (req, res) {
|
|
|
|
delete req.session.authUser
|
|
|
|
res.json({ ok: true })
|
2016-12-04 18:16:10 +00:00
|
|
|
})
|
|
|
|
|
2016-12-07 16:03:52 +00:00
|
|
|
// We instantiate Nuxt.js with the options
|
2016-12-07 16:13:32 +00:00
|
|
|
const isProd = process.env.NODE_ENV === 'production'
|
2016-12-08 15:39:27 +00:00
|
|
|
let config = require('./nuxt.config.js')
|
|
|
|
config.dev = !isProd
|
|
|
|
const nuxt = new Nuxt(config)
|
2016-12-07 18:23:08 +00:00
|
|
|
// No build in production
|
|
|
|
const promise = (isProd ? Promise.resolve() : nuxt.build())
|
|
|
|
promise.then(() => {
|
2016-12-04 18:16:10 +00:00
|
|
|
app.use(nuxt.render)
|
|
|
|
app.listen(3000)
|
2016-12-08 15:41:20 +00:00
|
|
|
console.log('Server is listening on http://localhost:3000') // eslint-disable-line no-console
|
2016-12-04 18:16:10 +00:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2016-12-08 15:41:20 +00:00
|
|
|
console.error(error) // eslint-disable-line no-console
|
2016-12-04 18:16:10 +00:00
|
|
|
process.exit(1)
|
|
|
|
})
|