https://github.com/jrop/express-dual
Split your express logic into separate data-fetching/view-rendering functions
https://github.com/jrop/express-dual
Last synced: 10 months ago
JSON representation
Split your express logic into separate data-fetching/view-rendering functions
- Host: GitHub
- URL: https://github.com/jrop/express-dual
- Owner: jrop
- Created: 2016-04-20T16:31:08.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2017-09-01T17:27:34.000Z (almost 9 years ago)
- Last Synced: 2025-10-08T23:06:49.276Z (10 months ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/express-dual
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
express-dual
============
> Split your express logic into separate data-fetching/view-rendering functions
# Installation
```sh
npm install --save express-dual
```
# Use
```js
...
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.
# API
## dual([ dataCallback, [ viewCallback ] ]) : DualMiddleware
> Create dual middleware
## DualMiddleware.data(dataCallback) : DualMiddleware
> 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`
## DualMiddleware.view(viewCallback) : DualMiddleware
> Attach a view-rendering function to middleware
The signature of `viewCallback` is `function (request, response, data, next)`