mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 14:15:13 +00:00
Use serverMiddleware instead of a module
This commit is contained in:
parent
1f7508d033
commit
1d77431db0
@ -1,38 +1,36 @@
|
|||||||
const express = require('express')
|
const express = require('express')
|
||||||
|
|
||||||
module.exports = function () {
|
// Create express router
|
||||||
// Create express router
|
const router = express.Router()
|
||||||
const router = express.Router()
|
|
||||||
|
|
||||||
var app = express()
|
// Transform req & res to have the same API as express
|
||||||
// Transform req & res to have the same API as express
|
// So we can use res.status() & res.json()
|
||||||
// So we can use res.status() & res.json()
|
var app = express()
|
||||||
router.use((req, res, next) => {
|
router.use((req, res, next) => {
|
||||||
Object.setPrototypeOf(req, app.request)
|
Object.setPrototypeOf(req, app.request)
|
||||||
Object.setPrototypeOf(res, app.response)
|
Object.setPrototypeOf(res, app.response)
|
||||||
req.res = res
|
req.res = res
|
||||||
res.req = req
|
res.req = req
|
||||||
next()
|
next()
|
||||||
})
|
})
|
||||||
|
|
||||||
// Add POST - /api/login
|
// Add POST - /api/login
|
||||||
router.post('/login', (req, res) => {
|
router.post('/login', (req, res) => {
|
||||||
if (req.body.username === 'demo' && req.body.password === 'demo') {
|
if (req.body.username === 'demo' && req.body.password === 'demo') {
|
||||||
req.session.authUser = { username: 'demo' }
|
req.session.authUser = { username: 'demo' }
|
||||||
return res.json({ username: 'demo' })
|
return res.json({ username: 'demo' })
|
||||||
}
|
}
|
||||||
res.status(401).json({ message: 'Bad credentials' })
|
res.status(401).json({ message: 'Bad credentials' })
|
||||||
})
|
})
|
||||||
|
|
||||||
// Add POST - /api/logout
|
// Add POST - /api/logout
|
||||||
router.post('/logout', (req, res) => {
|
router.post('/logout', (req, res) => {
|
||||||
delete req.session.authUser
|
delete req.session.authUser
|
||||||
res.json({ ok: true })
|
res.json({ ok: true })
|
||||||
})
|
})
|
||||||
|
|
||||||
// Add router to /api
|
// Export the server middleware
|
||||||
this.addServerMiddleware({
|
module.exports = {
|
||||||
path: '/api',
|
path: '/api',
|
||||||
handler: router
|
handler: router
|
||||||
})
|
|
||||||
}
|
}
|
@ -27,11 +27,9 @@ module.exports = {
|
|||||||
resave: false,
|
resave: false,
|
||||||
saveUninitialized: false,
|
saveUninitialized: false,
|
||||||
cookie: { maxAge: 60000 }
|
cookie: { maxAge: 60000 }
|
||||||
})
|
}),
|
||||||
],
|
// Api middleware
|
||||||
/*
|
// We add /api/login & /api/logout routes
|
||||||
** Add our custom module
|
'~/api'
|
||||||
** In this module we add /api/login & /api/logout routes
|
]
|
||||||
*/
|
|
||||||
modules: ['~/api']
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user