mirror of
https://github.com/nuxt/nuxt.git
synced 2024-12-18 10:17:16 +00:00
54 lines
1.4 KiB
JavaScript
54 lines
1.4 KiB
JavaScript
const Nuxt = require('../../')
|
|
const bodyParser = require('body-parser')
|
|
const session = require('express-session')
|
|
const app = require('express')()
|
|
const host = process.env.HOST || '127.0.0.1'
|
|
const port = process.env.PORT || '3000'
|
|
|
|
// Body parser, to access req.body
|
|
app.use(bodyParser.json())
|
|
|
|
// Sessions to create req.session
|
|
app.use(session({
|
|
secret: 'super-secret-key',
|
|
resave: false,
|
|
saveUninitialized: false,
|
|
cookie: { maxAge: 60000 }
|
|
}))
|
|
|
|
// 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({ message: 'Bad credentials' })
|
|
})
|
|
|
|
// 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 })
|
|
})
|
|
|
|
// Import and Set Nuxt.js options
|
|
let config = require('./nuxt.config.js')
|
|
config.dev = !(process.env.NODE_ENV === 'production')
|
|
|
|
// Init Nuxt.js
|
|
const nuxt = new Nuxt(config)
|
|
app.use(nuxt.render)
|
|
|
|
// Build only in dev mode
|
|
if (config.dev) {
|
|
nuxt.build()
|
|
.catch((error) => {
|
|
console.error(error) // eslint-disable-line no-console
|
|
process.exit(1)
|
|
})
|
|
}
|
|
|
|
// Listen the server
|
|
app.listen(port, host)
|
|
console.log('Server listening on ' + host + ':' + port) // eslint-disable-line no-console
|