Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

expressjs-plus

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

expressjs-plus

A pluggable expansion to express js aimed at adding much needed features and helpers.

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

ExpressPlus

Kind: global class

new ExpressPlus(app, passedParamHandlers, passedErrorHandlers)

ParamTypeDescription
appObjectexpress app object
passedParamHandlersArrayarray of functions in the format of @see lastHandler
passedErrorHandlersArrayarray 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

ParamTypeDescription
statusNumberHTTP error code
errCodeStringerrorCode, 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

ParamTypeDescription
funcfunctionthe function to be converted

Example

function regularFunc(someVar, cb){
    console.log(someVar);
    return cb(null, {response: someVar+="addedString"});
}
// middleware version of regularFunc
var func = GMV(regularFunc);

// func will behave like this
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

ParamTypeDescription
reqObjectrequest object
resObjectresponse object
res.statusfunctionfunction to set the status
res.localsObjectobject that is used to pass data around
res.locals.statusNumberContains HTTP status code
res.locals.responseObjectContains the response body
nextfunction

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

ParamTypeDefaultDescription
cbfunctioncallback function
[status]Number204optional 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

ParamTypeDescription
cbfunctioncallback function
[resource]Objectoptional argument to return instead of the actual result

expressPlus.useArray(middlewares)

Enables sending array of middlewares to app.use

Kind: instance method of ExpressPlus

Param
middlewares

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

ParamTypeDescription
paramStringstring parameter
paramsArrayArrayparameter arrays which will be sent to the underlying function of the middleware
reqObjectexpress request object that is used in middlewares

Keywords

FAQs

Package last updated on 11 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc