Split your express logic into separate data-fetching/view-rendering functions
npm install --save express-dual
...
const dual = require('express-dual')
const app = express()
app.get('/', dual()
.data(function (req) {
return { my: 'data' }
})
.view(function (req, res, data, next) {
res.render('index', data)
}))
When a request is made with the header Accept
set to application/json
, the middleware will respond with the data encoded as JSON, otherwise the middleware will call your view function.
Create dual middleware
Attach a data-fetching function to middleware
The dataCallback
function may return data immediately, or return a promise. The signature of dataCallback
is function (request) => Object|Promise
Attach a view-rendering function to middleware
The signature of viewCallback
is function (request, response, data, next)