utils.js
function extendHttpRequest(req) {
    req = req || express.request;

    /**
     * get param value from path-variable or query or body or headers.
     * @param {string} paramName
     * @param {*} [fallback]
     * @returns {*}
     */
    req.anyParam = function (paramName, fallback) {
        return (this.params && this.params[paramName]) || // path-variable
            (this.query && this.query[paramName]) ||
            (this.body && this.body[paramName]) ||
            (this.get && this.get(paramName)) || // header
            fallback;
    };

    /**
     * get string param from http request.
     *
     * @param {String} paramName
     * @param {String} [fallback]
     * @returns {String} param value
     * @throws {errors.BadRequest} no string param acquired and no fallback provided
     * @memberOf {express.request}
     */
    req.strParam = function (paramName, fallback) {
        var paramValue = this.anyParam(paramName);
        if (paramValue !== undefined) {
            return paramValue;
        }
        if (fallback !== undefined) {
            return fallback;
        }
        throw new errors.BadRequest('param_required:' + paramName);
    };

    /**
     * get integer param from http request.
     *
     * @param {String} paramName
     * @param {Number} [fallback]
     * @returns {Number} param value
     * @throws {errors.BadRequest} no integer param acquired and no fallback provided
     * @memberOf {express.request}
     */
    req.intParam = function (paramName, fallback) {
        var paramValue = utils.toInt(this.anyParam(paramName), fallback);
        if (paramValue !== undefined) {
            return paramValue;
        }
        if (fallback !== undefined) {
            return fallback;
        }
        throw new errors.BadRequest('int_param_required:' + paramName);
    };

    /**
     * get number(float) param from http request.
     *
     * @param {String} paramName
     * @param {Number} [fallback]
     * @returns {Number} param value
     * @throws {errors.BadRequest} no number param acquired and no fallback provided
     * @memberOf {express.request}
     */
    req.numberParam = function (paramName, fallback) {
        var paramValue = utils.toNumber(this.anyParam(paramName));
        if (paramValue !== undefined) {
            return paramValue;
        }
        if (fallback !== undefined) {
            return fallback;
        }
        throw new errors.BadRequest('number_param_required:' + paramName);
    };

    /**
     * get boolean param from http request.
     *
     * @param {String} paramName
     * @returns {Boolean} [fallback]
     * @returns {Boolean} param value
     * @throws {errors.BadRequest} no boolean param acquired and no fallback provided
     * @memberOf {express.request}
     */
    req.boolParam = function (paramName, fallback) {
        var paramValue = utils.toBoolean(this.anyParam(paramName));
        if (paramValue !== undefined) {
            return paramValue;
        }
        if (fallback !== undefined) {
            return fallback;
        }
        throw new errors.BadRequest('bool_param_required:' + paramName);
    };

    /**
     * get date param from http request.
     *
     * @param {String} paramName
     * @param {Date} [fallback]
     * @returns {Date} param value
     * @throws {errors.BadRequest} no date param acquired and no fallback provided
     * @memberOf {express.request}
     */
    req.dateParam = function (paramName, fallback) {
        var paramValue = Date.parse(this.anyParam(paramName));
        if (!isNaN(paramValue)) {
            return new Date(paramValue);
        }
        if (fallback !== undefined) {
            return fallback;
        }
        throw new errors.BadRequest('date_param_required:' + paramName);
    };

    /**
     * collect params from http request.
     *
     * @param {Array.<String>} paramNames
     * @returns {Object.<String,String>}
     */
    req.collectParams = function (paramNames) {
        var self = this;
        return paramNames.reduce(function (params, paramName) {
            var paramValue = self.strParam(paramName);
            if (paramValue) {
                params[paramName] = paramValue.trim();
            }
            return params;
        }, {});
    };
}
function extendHttpRequest(req) {
    req = req || express.request;

    /**
     * get param value from path-variable or query or body or headers.
     * @param {string} paramName
     * @param {*} [fallback]
     * @returns {*}
     */
    req.anyParam = function (paramName, fallback) {
        return (this.params && this.params[paramName]) || // path-variable
            (this.query && this.query[paramName]) ||
            (this.body && this.body[paramName]) ||
            (this.get && this.get(paramName)) || // header
            fallback;
    };

    /**
     * get string param from http request.
     *
     * @param {String} paramName
     * @param {String} [fallback]
     * @returns {String} param value
     * @throws {errors.BadRequest} no string param acquired and no fallback provided
     * @memberOf {express.request}
     */
    req.strParam = function (paramName, fallback) {
        var paramValue = this.anyParam(paramName);
        if (paramValue !== undefined) {
            return paramValue;
        }
        if (fallback !== undefined) {
            return fallback;
        }
        throw new errors.BadRequest('param_required:' + paramName);
    };

    /**
     * get integer param from http request.
     *
     * @param {String} paramName
     * @param {Number} [fallback]
     * @returns {Number} param value
     * @throws {errors.BadRequest} no integer param acquired and no fallback provided
     * @memberOf {express.request}
     */
    req.intParam = function (paramName, fallback) {
        var paramValue = utils.toInt(this.anyParam(paramName), fallback);
        if (paramValue !== undefined) {
            return paramValue;
        }
        if (fallback !== undefined) {
            return fallback;
        }
        throw new errors.BadRequest('int_param_required:' + paramName);
    };

    /**
     * get number(float) param from http request.
     *
     * @param {String} paramName
     * @param {Number} [fallback]
     * @returns {Number} param value
     * @throws {errors.BadRequest} no number param acquired and no fallback provided
     * @memberOf {express.request}
     */
    req.numberParam = function (paramName, fallback) {
        var paramValue = utils.toNumber(this.anyParam(paramName));
        if (paramValue !== undefined) {
            return paramValue;
        }
        if (fallback !== undefined) {
            return fallback;
        }
        throw new errors.BadRequest('number_param_required:' + paramName);
    };

    /**
     * get boolean param from http request.
     *
     * @param {String} paramName
     * @returns {Boolean} [fallback]
     * @returns {Boolean} param value
     * @throws {errors.BadRequest} no boolean param acquired and no fallback provided
     * @memberOf {express.request}
     */
    req.boolParam = function (paramName, fallback) {
        var paramValue = utils.toBoolean(this.anyParam(paramName));
        if (paramValue !== undefined) {
            return paramValue;
        }
        if (fallback !== undefined) {
            return fallback;
        }
        throw new errors.BadRequest('bool_param_required:' + paramName);
    };

    /**
     * get date param from http request.
     *
     * @param {String} paramName
     * @param {Date} [fallback]
     * @returns {Date} param value
     * @throws {errors.BadRequest} no date param acquired and no fallback provided
     * @memberOf {express.request}
     */
    req.dateParam = function (paramName, fallback) {
        var paramValue = Date.parse(this.anyParam(paramName));
        if (!isNaN(paramValue)) {
            return new Date(paramValue);
        }
        if (fallback !== undefined) {
            return fallback;
        }
        throw new errors.BadRequest('date_param_required:' + paramName);
    };

    /**
     * collect params from http request.
     *
     * @param {Array.<String>} paramNames
     * @returns {Object.<String,String>}
     */
    req.collectParams = function (paramNames) {
        var self = this;
        return paramNames.reduce(function (params, paramName) {
            var paramValue = self.strParam(paramName);
            if (paramValue) {
                params[paramName] = paramValue.trim();
            }
            return params;
        }, {});
    };
}
function extendHttpRequest ( req ) { req = req || express . request ; /**
     * get param value from path-variable or query or body or headers.
     * @param {string} paramName
     * @param {*} [fallback]
     * @returns {*}
     */ req . anyParam = function ( paramName , fallback ) { return ( this . params && this . params [ paramName ] ) || // path-variable ( this . query && this . query [ paramName ] ) || ( this . body && this . body [ paramName ] ) || ( this . get && this . get ( paramName ) ) || // header fallback ; } ; /**
     * get string param from http request.
     *
     * @param {String} paramName
     * @param {String} [fallback]
     * @returns {String} param value
     * @throws {errors.BadRequest} no string param acquired and no fallback provided
     * @memberOf {express.request}
     */ req . strParam = function ( paramName , fallback ) { var paramValue = this . anyParam ( paramName ) ; if ( paramValue !== undefined ) { return paramValue ; } if ( fallback !== undefined ) { return fallback ; } throw new errors . BadRequest ( 'param_required:' + paramName ) ; } ; /**
     * get integer param from http request.
     *
     * @param {String} paramName
     * @param {Number} [fallback]
     * @returns {Number} param value
     * @throws {errors.BadRequest} no integer param acquired and no fallback provided
     * @memberOf {express.request}
     */ req . intParam = function ( paramName , fallback ) { var paramValue = utils . toInt ( this . anyParam ( paramName ) , fallback ) ; if ( paramValue !== undefined ) { return paramValue ; } if ( fallback !== undefined ) { return fallback ; } throw new errors . BadRequest ( 'int_param_required:' + paramName ) ; } ; /**
     * get number(float) param from http request.
     *
     * @param {String} paramName
     * @param {Number} [fallback]
     * @returns {Number} param value
     * @throws {errors.BadRequest} no number param acquired and no fallback provided
     * @memberOf {express.request}
     */ req . numberParam = function ( paramName , fallback ) { var paramValue = utils . toNumber ( this . anyParam ( paramName ) ) ; if ( paramValue !== undefined ) { return paramValue ; } if ( fallback !== undefined ) { return fallback ; } throw new errors . BadRequest ( 'number_param_required:' + paramName ) ; } ; /**
     * get boolean param from http request.
     *
     * @param {String} paramName
     * @returns {Boolean} [fallback]
     * @returns {Boolean} param value
     * @throws {errors.BadRequest} no boolean param acquired and no fallback provided
     * @memberOf {express.request}
     */ req . boolParam = function ( paramName , fallback ) { var paramValue = utils . toBoolean ( this . anyParam ( paramName ) ) ; if ( paramValue !== undefined ) { return paramValue ; } if ( fallback !== undefined ) { return fallback ; } throw new errors . BadRequest ( 'bool_param_required:' + paramName ) ; } ; /**
     * get date param from http request.
     *
     * @param {String} paramName
     * @param {Date} [fallback]
     * @returns {Date} param value
     * @throws {errors.BadRequest} no date param acquired and no fallback provided
     * @memberOf {express.request}
     */ req . dateParam = function ( paramName , fallback ) { var paramValue = Date . parse ( this . anyParam ( paramName ) ) ; if ( ! isNaN ( paramValue ) ) { return new Date ( paramValue ) ; } if ( fallback !== undefined ) { return fallback ; } throw new errors . BadRequest ( 'date_param_required:' + paramName ) ; } ; /**
     * collect params from http request.
     *
     * @param {Array.<String>} paramNames
     * @returns {Object.<String,String>}
     */ req . collectParams = function ( paramNames ) { var self = this ; return paramNames . reduce ( function ( params , paramName ) { var paramValue = self . strParam ( paramName ) ; if ( paramValue ) { params [ paramName ] = paramValue . trim ( ) ; } return params ; } , { } ) ; } ; }
add some utility methods to http request.

@param {*} [req]
add some utility methods to http request .