-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c49aa2e
Showing
9 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
### Purpose | ||
This middleware adds two properties to express's `res` object: | ||
* `promises` - empty object for other middlewares to store promises on | ||
* `renderAfterPromises` - method which waits for all promises on `res.promises` to resolve before calling `res.render` with promise resolutions | ||
|
||
### Install | ||
`npm install res-promises` | ||
|
||
### Use | ||
``` | ||
const express = require('express'); | ||
const resPromises = require('res-promises'); | ||
const app = express(); | ||
app.use(resPromises()) | ||
``` | ||
See `example` dir for how to use with other middleware. | ||
|
||
### Options | ||
* `resPromises([overrideResRender])` - boolean to override `res`'s `render` method. Default method name is `renderAfterPromises` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
const Promise = require('bluebird'); | ||
|
||
|
||
module.exports = headingMiddleware; | ||
|
||
|
||
function headingMiddleware(req, res, next) { | ||
res.promises.heading = new Promise(function(resolve, reject) { | ||
setTimeout(function() { | ||
resolve('Waited for data before rendering!'); | ||
}, 2000); | ||
}); | ||
|
||
next(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
|
||
const express = require('express'); | ||
const resPromises = require('../index.js'); | ||
const titleMiddleware = require('./title.middleware.promise'); | ||
const headingMiddleware = require('./heading.middleware.promise'); | ||
const viewMiddleware = require('./view.middleware.render'); | ||
|
||
const app = express(); | ||
|
||
app.use( | ||
resPromises(true), | ||
titleMiddleware, | ||
headingMiddleware, | ||
viewMiddleware | ||
); | ||
|
||
app.listen(3000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
doctype html | ||
html | ||
head | ||
title= title | ||
body | ||
h1= heading |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
const Promise = require('bluebird'); | ||
|
||
|
||
module.exports = titleMiddleware; | ||
|
||
|
||
function titleMiddleware(req, res, next) { | ||
res.promises.title = new Promise(function(resolve, reject) { | ||
setTimeout(function() { | ||
resolve('res promises example'); | ||
}, 1000); | ||
}); | ||
|
||
next(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
const template = require.resolve('./index.pug'); | ||
|
||
|
||
module.exports = viewMiddleware; | ||
|
||
|
||
function viewMiddleware(req, res, next) { | ||
res.renderAfterPromises(template); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
|
||
const BB = require('bluebird'); | ||
const _ = require('lodash'); | ||
|
||
|
||
module.exports = resPromises; | ||
|
||
|
||
function resPromises(overrideResRender) { | ||
var renderMethodName = overrideResRender ? 'render' : 'renderAfterPromises'; | ||
|
||
return function(req, res, next) { | ||
// attach to this object anything you want to wait to render for | ||
res.promises = {}; | ||
|
||
var render = res.render; | ||
|
||
res[renderMethodName] = function(template, viewModel) { | ||
// wait for all promises attached to resolve before calling render with resolutions | ||
BB.props(res.promises) | ||
.then(function(promiseResolutions) { | ||
render.call(res, template, _.extend({}, promiseResolutions, viewModel)); | ||
}); | ||
}; | ||
|
||
next(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "res-promises", | ||
"version": "1.0.0", | ||
"description": "Express middleware that adds promises object and render method that waits for promises", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/lukechinworth/res-promises.git" | ||
}, | ||
"keywords": [ | ||
"express", | ||
"middleware", | ||
"promise" | ||
], | ||
"author": "Luke Chinworth", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/lukechinworth/res-promises/issues" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"homepage": "https://github.com/lukechinworth/res-promises#readme", | ||
"dependencies": { | ||
"bluebird": "^3.4.7", | ||
"lodash": "^4.17.4" | ||
}, | ||
"devDependencies": { | ||
"express": "^4.14.1", | ||
"pug": "^2.0.0-beta11" | ||
} | ||
} |