ExpressPlus
Kind: global class
new ExpressPlus(app, passedParamHandlers, passedErrorHandlers)
Param | Type | Description |
---|
app | Object | express app object |
passedParamHandlers | Array | array of functions in the format of @see lastHandler |
passedErrorHandlers | Array | array of middlewares |
Example
// Usage
var express = require('express');
var bodyParser = require('body-parser');
var userHandler = function(param, paramsArray, req){
if(param !== 'user') return false;
paramsArray.push(req.user);
return true;
};
var app = express()
app.use(bodyParser.json());
var appPlus = new ExpressPlus(app, [userHandler], []);
var regularFunction = function(user, id, cb){
return cb(null, { response: {user: user, id: id}, status: 200 });
};
app.use(appPlus.GMV(regularFunction), appPlus.responder);
appPlus.setErrorHandlers();
Example
//this is an example of a paramHandler function that is interested in the user parameter
function userHandler(param, paramsArray, req){
if(param === 'user'){
paramsArray.push(req.user);
return true;
}else{
return false;
}
}
expressPlus.HTTPError
Kind: instance class of ExpressPlus
new this.HTTPError(status, errCode)
Generic error handler
Param | Type | Description |
---|
status | Number | HTTP error code |
errCode | String | errorCode, the error handler should handle this |
expressPlus.getMiddlewareVersion ⇒
Returns a middleware version of the function passed, this function replaces the last parameter with a callback
function to work with express js.
Kind: instance property of ExpressPlus
Returns: function
Param | Type | Description |
---|
func | function | the function to be converted |
Example
function regularFunc(someVar, cb){
console.log(someVar);
return cb(null, {response: someVar+="addedString"});
}
var func = GMV(regularFunc);
function mw(req, res, next){
let someVar = req.query.someVar;
console.log(someVar);
res.locals.response = someVar+="addedString";
return next();
}
expressPlus.setErrorHandlers()
sets error handlers, make sure to use this last
Kind: instance method of ExpressPlus
expressPlus.responder(req, res, next)
Handles responses. Other middlewares need to use locals to pass data to this function
Kind: instance method of ExpressPlus
Param | Type | Description |
---|
req | Object | request object |
res | Object | response object |
res.status | function | function to set the status |
res.locals | Object | object that is used to pass data around |
res.locals.status | Number | Contains HTTP status code |
res.locals.response | Object | Contains the response body |
next | function | |
expressPlus.defaultCbWithResponse(cb, [status])
Handles callbacks and puts response & status in the second callback argument if successful
Replace your callback with this if appropriate.
Kind: instance method of ExpressPlus
Param | Type | Default | Description |
---|
cb | function | | callback function |
[status] | Number | 204 | optional argument to pass specific HTTP code if no errors were found if the status is 204, no response will be returns according to HTTP codes. |
expressPlus.defaultCb(cb, [resource])
Handles callbacks.
Replace your callback with this if appropriate.
Kind: instance method of ExpressPlus
Param | Type | Description |
---|
cb | function | callback function |
[resource] | Object | optional argument to return instead of the actual result |
expressPlus.useArray(middlewares)
Enables sending array of middlewares to app.use
Kind: instance method of ExpressPlus
ExpressPlus~lastHandler(param, paramsArray, req) ⇒ boolean
Default parameter handler used in getMiddlewareVersion.
Every parameter is passed to a set of functions to be handled, this is the last handler that just pushes
the parameter to the paramsArray.
Kind: inner method of ExpressPlus
Returns: boolean
- if true is returned, the parameter will be considered handled and the function GMV will
move on to the next parameter. if false is returned, the next handler on the list will attempt to handle the
parameter until this methods turn comes, which will always return true
See: dataHandler this function is a more real example of a parameter handler, it is used to integrate
with another library https://www.npmjs.com/package/simple-express-validator
Param | Type | Description |
---|
param | String | string parameter |
paramsArray | Array | parameter arrays which will be sent to the underlying function of the middleware |
req | Object | express request object that is used in middlewares |