Use serverMiddleware instead of a module

This commit is contained in:
Sébastien Chopin 2017-07-08 18:09:47 +02:00
parent 1f7508d033
commit 1d77431db0
2 changed files with 34 additions and 38 deletions

View File

@ -1,12 +1,11 @@
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)
@ -30,9 +29,8 @@ module.exports = function () {
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
})
} }

View File

@ -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']
} }