utils.js
function extendHttpResponse(res) {
    res = res || express.response;

    function callbackFnFn(verbFunc) {
        return function (next, status) {
            var res = this;
            return function (err, result) {
                if (err) {
                    return next(err);
                }
                if (status) {
                    return res.status(status);
                }
                return verbFunc.call(res, result);
            };
        };
    }

    function laterFnFn(verbFunc) {
        return function (promise, next, status) {
            var res = this;
            return Q.when(promise).then(function (result) {
                if (status) {
                    res.status(status);
                }
                return verbFunc.call(res, result);
            }).fail(next).done();
        };
    }

    //
    // callback helpers
    //

    /**
     * create generic node.js callback function to invoke 'express.response.send()'.
     *
     * ex.
     * ```
     * var fs = require('fs');
     * app.get('/foo', function(req, res, next) {
     *   fs.readFile('foo.txt', res.sendCallbackFn(next));
     * });
     * ```
     * @param {function} next
     * @param {number} [status]
     * @returns {function(err, result)} generic node.js callback which send response.
     * @method
     * @memberOf {express.request}
     */
    res.sendCallbackFn = callbackFnFn(res.send);

    /**
     * create generic node.js callback function to invoke 'express.response.json()'.
     *
     * @param {function} next
     * @param {number} [status]
     * @returns {function(err, result)} generic node.js callback which send 'json' response.
     * @method
     * @memberOf {express.request}
     */
    res.jsonCallbackFn = callbackFnFn(res.json);

    /**
     * create generic node.js callback function to invoke 'express.response.jsonp()'.
     *
     * @param {function} next
     * @param {number} [status]
     * @returns {function(err, result)} generic node.js callback which send 'jsonp' response.
     * @method
     * @memberOf {express.request}
     */
    res.jsonpCallbackFn = callbackFnFn(res.jsonp);

    /**
     * create generic node.js callback function to invoke 'express.response.sendFile()'.
     *
     * @param {function} next
     * @param {number} [status]
     * @returns {function(err, result)} generic node.js callback which send 'sendFile' response.
     * @method
     * @memberOf {express.request}
     */
    res.sendFileCallbackFn = callbackFnFn(res.sendFile);

    /**
     * create generic node.js callback function to invoke 'express.response.redirect()'.
     *
     * @param {function} next
     * @param {number} [status]
     * @returns {function(err, result)} generic node.js callback which send 'redirect' response.
     * @method
     * @memberOf {express.request}
     */
    res.redirectCallbackFn = callbackFnFn(res.redirect);

    /**
     * ex.
     * ```
     * fs.stat('foo.txt', res.renderCallbackFn('stat_view', next));
     * ```
     *
     * @param {string} view
     * @param {function} next
     * @param {number} [status]
     * @returns {function(err, result)}
     * @memberOf {express.response}
     */
    res.renderCallbackFn = function (view, next, status) {
        var res = this;
        return function (err, result) {
            if (err) {
                return next(err);
            }
            if (status) {
                res.status(status);
            }
            return res.render(view, result);
        };
    };

    //
    // promise helpers
    //

    /**
     * promise version to invoke `express.response.send()`.
     *
     * ex.
     * ```
     * var FS = require('q-io/fs');
     * app.get('/foo', function (req, res, next) {
     *   res.sendLater(FS.readFile('foo.txt'), next);
     * })
     * ```
     *
     * @param {promise|*} promise of response.
     * @param {function} next
     * @param {number} [status]
     * @method
     * @memberOf {express.request}
     */
    res.sendLater = laterFnFn(res.send);

    /**
     * promise version of `express.response.json()`.
     *
     * @param {promise|*} promise of 'json' response.
     * @param {function} next
     * @param {number} [status]
     * @method
     * @memberOf {express.request}
     */
    res.jsonLater = laterFnFn(res.json);

    /**
     * promise version of `express.response.jsonp()`.
     *
     * @param {promise|*} promise of 'jsonp' response.
     * @param {function} next
     * @param {number} [status]
     * @method
     * @memberOf {express.request}
     */
    res.jsonpLater = laterFnFn(res.jsonp);

    /**
     * promise version of `express.response.sendFile()`.
     *
     * @param {promise|*} promise of 'sendFile' response.
     * @param {function} next
     * @param {number} [status]
     * @method
     * @memberOf {express.request}
     */
    res.sendFileLater = laterFnFn(res.sendFile);

    /**
     * promise version of `express.response.redirect()`.
     *
     * @param {promise|*} promise of 'redirect' response.
     * @param {function} next
     * @param {number} [status]
     * @method
     * @memberOf {express.request}
     */
    res.redirectLater = laterFnFn(res.redirect);

    /**
     *
     * ex.
     * ```
     * var FS = require('q-io/fs');
     * res.renderLater('stat_view', FS.stat('foo.txt'), next);
     * ```
     *
     * @param {string} view
     * @param {promise|*} promise of 'render' model.
     * @param {function} next
     * @param {number} [status]
     * @memberOf {express.response}
     */
    res.renderLater = function (view, promise, next, status) {
        var res = this;
        return Q.when(promise).then(function (result) {
            if (status) {
                res.status(status);
            }
            return res.render(view, result);
        }).fail(next).done();
    };
}
function extendHttpResponse(res) {
    res = res || express.response;

    function callbackFnFn(verbFunc) {
        return function (next, status) {
            var res = this;
            return function (err, result) {
                if (err) {
                    return next(err);
                }
                if (status) {
                    return res.status(status);
                }
                return verbFunc.call(res, result);
            };
        };
    }

    function laterFnFn(verbFunc) {
        return function (promise, next, status) {
            var res = this;
            return Q.when(promise).then(function (result) {
                if (status) {
                    res.status(status);
                }
                return verbFunc.call(res, result);
            }).fail(next).done();
        };
    }

    //
    // callback helpers
    //

    /**
     * create generic node.js callback function to invoke 'express.response.send()'.
     *
     * ex.
     * ```
     * var fs = require('fs');
     * app.get('/foo', function(req, res, next) {
     *   fs.readFile('foo.txt', res.sendCallbackFn(next));
     * });
     * ```
     * @param {function} next
     * @param {number} [status]
     * @returns {function(err, result)} generic node.js callback which send response.
     * @method
     * @memberOf {express.request}
     */
    res.sendCallbackFn = callbackFnFn(res.send);

    /**
     * create generic node.js callback function to invoke 'express.response.json()'.
     *
     * @param {function} next
     * @param {number} [status]
     * @returns {function(err, result)} generic node.js callback which send 'json' response.
     * @method
     * @memberOf {express.request}
     */
    res.jsonCallbackFn = callbackFnFn(res.json);

    /**
     * create generic node.js callback function to invoke 'express.response.jsonp()'.
     *
     * @param {function} next
     * @param {number} [status]
     * @returns {function(err, result)} generic node.js callback which send 'jsonp' response.
     * @method
     * @memberOf {express.request}
     */
    res.jsonpCallbackFn = callbackFnFn(res.jsonp);

    /**
     * create generic node.js callback function to invoke 'express.response.sendFile()'.
     *
     * @param {function} next
     * @param {number} [status]
     * @returns {function(err, result)} generic node.js callback which send 'sendFile' response.
     * @method
     * @memberOf {express.request}
     */
    res.sendFileCallbackFn = callbackFnFn(res.sendFile);

    /**
     * create generic node.js callback function to invoke 'express.response.redirect()'.
     *
     * @param {function} next
     * @param {number} [status]
     * @returns {function(err, result)} generic node.js callback which send 'redirect' response.
     * @method
     * @memberOf {express.request}
     */
    res.redirectCallbackFn = callbackFnFn(res.redirect);

    /**
     * ex.
     * ```
     * fs.stat('foo.txt', res.renderCallbackFn('stat_view', next));
     * ```
     *
     * @param {string} view
     * @param {function} next
     * @param {number} [status]
     * @returns {function(err, result)}
     * @memberOf {express.response}
     */
    res.renderCallbackFn = function (view, next, status) {
        var res = this;
        return function (err, result) {
            if (err) {
                return next(err);
            }
            if (status) {
                res.status(status);
            }
            return res.render(view, result);
        };
    };

    //
    // promise helpers
    //

    /**
     * promise version to invoke `express.response.send()`.
     *
     * ex.
     * ```
     * var FS = require('q-io/fs');
     * app.get('/foo', function (req, res, next) {
     *   res.sendLater(FS.readFile('foo.txt'), next);
     * })
     * ```
     *
     * @param {promise|*} promise of response.
     * @param {function} next
     * @param {number} [status]
     * @method
     * @memberOf {express.request}
     */
    res.sendLater = laterFnFn(res.send);

    /**
     * promise version of `express.response.json()`.
     *
     * @param {promise|*} promise of 'json' response.
     * @param {function} next
     * @param {number} [status]
     * @method
     * @memberOf {express.request}
     */
    res.jsonLater = laterFnFn(res.json);

    /**
     * promise version of `express.response.jsonp()`.
     *
     * @param {promise|*} promise of 'jsonp' response.
     * @param {function} next
     * @param {number} [status]
     * @method
     * @memberOf {express.request}
     */
    res.jsonpLater = laterFnFn(res.jsonp);

    /**
     * promise version of `express.response.sendFile()`.
     *
     * @param {promise|*} promise of 'sendFile' response.
     * @param {function} next
     * @param {number} [status]
     * @method
     * @memberOf {express.request}
     */
    res.sendFileLater = laterFnFn(res.sendFile);

    /**
     * promise version of `express.response.redirect()`.
     *
     * @param {promise|*} promise of 'redirect' response.
     * @param {function} next
     * @param {number} [status]
     * @method
     * @memberOf {express.request}
     */
    res.redirectLater = laterFnFn(res.redirect);

    /**
     *
     * ex.
     * ```
     * var FS = require('q-io/fs');
     * res.renderLater('stat_view', FS.stat('foo.txt'), next);
     * ```
     *
     * @param {string} view
     * @param {promise|*} promise of 'render' model.
     * @param {function} next
     * @param {number} [status]
     * @memberOf {express.response}
     */
    res.renderLater = function (view, promise, next, status) {
        var res = this;
        return Q.when(promise).then(function (result) {
            if (status) {
                res.status(status);
            }
            return res.render(view, result);
        }).fail(next).done();
    };
}
function extendHttpResponse ( res ) { res = res || express . response ; function callbackFnFn ( verbFunc ) { return function ( next , status ) { var res = this ; return function ( err , result ) { if ( err ) { return next ( err ) ; } if ( status ) { return res . status ( status ) ; } return verbFunc . call ( res , result ) ; } ; } ; } function laterFnFn ( verbFunc ) { return function ( promise , next , status ) { var res = this ; return Q . when ( promise ) . then ( function ( result ) { if ( status ) { res . status ( status ) ; } return verbFunc . call ( res , result ) ; } ) . fail ( next ) . done ( ) ; } ; } // // callback helpers // /**
     * create generic node.js callback function to invoke 'express.response.send()'.
     *
     * ex.
     * ```
     * var fs = require('fs');
     * app.get('/foo', function(req, res, next) {
     *   fs.readFile('foo.txt', res.sendCallbackFn(next));
     * });
     * ```
     * @param {function} next
     * @param {number} [status]
     * @returns {function(err, result)} generic node.js callback which send response.
     * @method
     * @memberOf {express.request}
     */ res . sendCallbackFn = callbackFnFn ( res . send ) ; /**
     * create generic node.js callback function to invoke 'express.response.json()'.
     *
     * @param {function} next
     * @param {number} [status]
     * @returns {function(err, result)} generic node.js callback which send 'json' response.
     * @method
     * @memberOf {express.request}
     */ res . jsonCallbackFn = callbackFnFn ( res . json ) ; /**
     * create generic node.js callback function to invoke 'express.response.jsonp()'.
     *
     * @param {function} next
     * @param {number} [status]
     * @returns {function(err, result)} generic node.js callback which send 'jsonp' response.
     * @method
     * @memberOf {express.request}
     */ res . jsonpCallbackFn = callbackFnFn ( res . jsonp ) ; /**
     * create generic node.js callback function to invoke 'express.response.sendFile()'.
     *
     * @param {function} next
     * @param {number} [status]
     * @returns {function(err, result)} generic node.js callback which send 'sendFile' response.
     * @method
     * @memberOf {express.request}
     */ res . sendFileCallbackFn = callbackFnFn ( res . sendFile ) ; /**
     * create generic node.js callback function to invoke 'express.response.redirect()'.
     *
     * @param {function} next
     * @param {number} [status]
     * @returns {function(err, result)} generic node.js callback which send 'redirect' response.
     * @method
     * @memberOf {express.request}
     */ res . redirectCallbackFn = callbackFnFn ( res . redirect ) ; /**
     * ex.
     * ```
     * fs.stat('foo.txt', res.renderCallbackFn('stat_view', next));
     * ```
     *
     * @param {string} view
     * @param {function} next
     * @param {number} [status]
     * @returns {function(err, result)}
     * @memberOf {express.response}
     */ res . renderCallbackFn = function ( view , next , status ) { var res = this ; return function ( err , result ) { if ( err ) { return next ( err ) ; } if ( status ) { res . status ( status ) ; } return res . render ( view , result ) ; } ; } ; // // promise helpers // /**
     * promise version to invoke `express.response.send()`.
     *
     * ex.
     * ```
     * var FS = require('q-io/fs');
     * app.get('/foo', function (req, res, next) {
     *   res.sendLater(FS.readFile('foo.txt'), next);
     * })
     * ```
     *
     * @param {promise|*} promise of response.
     * @param {function} next
     * @param {number} [status]
     * @method
     * @memberOf {express.request}
     */ res . sendLater = laterFnFn ( res . send ) ; /**
     * promise version of `express.response.json()`.
     *
     * @param {promise|*} promise of 'json' response.
     * @param {function} next
     * @param {number} [status]
     * @method
     * @memberOf {express.request}
     */ res . jsonLater = laterFnFn ( res . json ) ; /**
     * promise version of `express.response.jsonp()`.
     *
     * @param {promise|*} promise of 'jsonp' response.
     * @param {function} next
     * @param {number} [status]
     * @method
     * @memberOf {express.request}
     */ res . jsonpLater = laterFnFn ( res . jsonp ) ; /**
     * promise version of `express.response.sendFile()`.
     *
     * @param {promise|*} promise of 'sendFile' response.
     * @param {function} next
     * @param {number} [status]
     * @method
     * @memberOf {express.request}
     */ res . sendFileLater = laterFnFn ( res . sendFile ) ; /**
     * promise version of `express.response.redirect()`.
     *
     * @param {promise|*} promise of 'redirect' response.
     * @param {function} next
     * @param {number} [status]
     * @method
     * @memberOf {express.request}
     */ res . redirectLater = laterFnFn ( res . redirect ) ; /**
     *
     * ex.
     * ```
     * var FS = require('q-io/fs');
     * res.renderLater('stat_view', FS.stat('foo.txt'), next);
     * ```
     *
     * @param {string} view
     * @param {promise|*} promise of 'render' model.
     * @param {function} next
     * @param {number} [status]
     * @memberOf {express.response}
     */ res . renderLater = function ( view , promise , next , status ) { var res = this ; return Q . when ( promise ) . then ( function ( result ) { if ( status ) { res . status ( status ) ; } return res . render ( view , result ) ; } ) . fail ( next ) . done ( ) ; } ; }
add some utility methods to http response.

@param {*} [res]
add some utility methods to http response .