express-promise
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -132,4 +132,4 @@ var resolveAsync = function(object, callback, maxDeep, currentDeep) { | ||
if (~options.methods.indexOf('send')) { | ||
var originalResSend = res.json.bind(res); | ||
res.json = function() { | ||
var originalResSend = res.send.bind(res); | ||
res.send = function() { | ||
var args = arguments; | ||
@@ -139,3 +139,3 @@ var body = args[0]; | ||
if (2 === args.length) { | ||
// res.json(body, status) backwards compat | ||
// res.send(body, status) backwards compat | ||
if ('number' === typeof args[1]) { | ||
@@ -142,0 +142,0 @@ status = args[1]; |
{ | ||
"name": "express-promise", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"scripts": { | ||
@@ -5,0 +5,0 @@ "test": "mocha -R spec" |
116
README.md
# express-promise | ||
An [express.js](http://expressjs.com) middleware for easy rendering async query. | ||
[![Build Status](https://travis-ci.org/luin/express-promise.png?branch=master)](https://travis-ci.org/luin/express-promise) | ||
## Cases | ||
@@ -29,46 +31,74 @@ ### 1. previously | ||
app.get('/project/:projectId', function(req, res) { | ||
var field = req.query.fields.split(';'); | ||
var result = {}; | ||
var pending = 0; | ||
if (field.indexOf('people') !== -1) { | ||
pending++; | ||
Project.getField(req.params.projectId).then(function(result) { | ||
result.people = result; | ||
if (--pending) { | ||
output(); | ||
} | ||
}); | ||
} | ||
if (field.indexOf('tasks') !== -1) { | ||
pending++; | ||
Project.getTaskCount(req.params.projectId).then(function(result) { | ||
result.tasksCount= result; | ||
if (--pending) { | ||
output(); | ||
} | ||
}); | ||
} | ||
function output() { | ||
res.json(result); | ||
} | ||
}); | ||
app.get('/project/:projectId', function(req, res) { | ||
var field = req.query.fields.split(';'); | ||
var result = {}; | ||
var pending = 0; | ||
if (field.indexOf('people') !== -1) { | ||
pending++; | ||
Project.getField(req.params.projectId).then(function(result) { | ||
result.people = result; | ||
if (--pending) { | ||
output(); | ||
} | ||
}); | ||
} | ||
if (field.indexOf('tasks') !== -1) { | ||
pending++; | ||
Project.getTaskCount(req.params.projectId).then(function(result) { | ||
result.tasksCount= result; | ||
if (--pending) { | ||
output(); | ||
} | ||
}); | ||
} | ||
function output() { | ||
res.json(result); | ||
} | ||
}); | ||
### 2. now | ||
app.get('/project/:projectId', function(req, res) { | ||
var field = req.query.fields.split(';'); | ||
var result = {}; | ||
if (field.indexOf('people') !== -1) { | ||
result.people = Project.getField(req.params.projectId); | ||
} | ||
if (field.indexOf('tasks') !== -1) { | ||
result.tasksCount = Project.getTaskCount(req.params.projectId); | ||
} | ||
res.json(result); | ||
}); | ||
app.get('/project/:projectId', function(req, res) { | ||
var field = req.query.fields.split(';'); | ||
var result = {}; | ||
if (field.indexOf('people') !== -1) { | ||
result.people = Project.getField(req.params.projectId); | ||
} | ||
if (field.indexOf('tasks') !== -1) { | ||
result.tasksCount = Project.getTaskCount(req.params.projectId); | ||
} | ||
res.json(result); | ||
}); | ||
## Install | ||
$ npm install express-promise | ||
## Usage | ||
app.use(require('express-promise')()); | ||
## License | ||
The MIT License (MIT) | ||
Copyright (c) 2013 Zihua Li | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
@@ -8,2 +8,3 @@ require('./spec_helper'); | ||
json: function(body) { | ||
arguments.should.have.length(1); | ||
body.should.equal('hi'); | ||
@@ -21,2 +22,3 @@ done(); | ||
json: function(body) { | ||
arguments.should.have.length(1); | ||
body.promise.should.equal('hi'); | ||
@@ -37,3 +39,22 @@ done(); | ||
it('should support two arguments', function(done) { | ||
var res = { | ||
json: function(status, body) { | ||
arguments.should.have.length(2); | ||
status.should.equal(200); | ||
body.promise.should.equal('hi'); | ||
done(); | ||
} | ||
}; | ||
expressPromise({methods: ['json']})(null, res); | ||
function async(callback) { | ||
callback(null, 'hi'); | ||
} | ||
res.json(200, { | ||
promise: async.promise() | ||
}); | ||
}); | ||
}); | ||
14732
12
359
104