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

express-exitware

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-exitware

Allows a standardised exit from controllers so you can standardise behaviour across your application, with access to req, res and next.

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7
increased by600%
Maintainers
1
Weekly downloads
 
Created
Source

Express Exitware

Allows a standardised exit from controllers so you can standardise behaviour across your application, with access to req, res and next.

Includes by default:

res.ok(someResponse); //For a 200 response with a body
res.ok(someObject, true) //for a 200 JSON response
res.badRequest(responseText); //for a 400 response
res.forbidden(responseText); //for a 403 response
res.notFound(responseText); //for a 404 response
res.serverError(responseText); //for a 500 response

Installation

Install the package via npm:

$ npm install express-exitware

Usage

Declare either basic exitware, or with a path to more exitware:

var exitware = require("express-exitware");
app.use(exitware());
//or
app.use(exitware(__dirname + "/./explicitPathToMyExitwares");

Adding Exitware

Adding exitware is very simple, just create a folder and name your files based on the response you want.

Example:

You want to standardise your response for bad parameters and also log any bad parameters server side for future debugging. You make a folder called responses and inside it you create a file called badParameters.js (or a folder called badParameters containing an index.js).

The file might look like:

module.exports = function(error){
    var log = require("winston"); //Your error logger of choice
    var sentParameters = this.req.params; //Any parameters sent you can get form the req object.
    var sentBody = this.req.body;
    error.sentParameters = sentParameters; //Add the extra information to your error object you're logging
    error.sentBody = sentBody; //As above
    var messageToSend = error.message; //Get the message from the error message you passed in
    log.error(error); //log the error for future debugging
    this.res.status(400).send(messageToSend); //Send the message as a 400 response 
};

You attach exitware to your application as described at the top

var exitware = require("express-exitware");
app.use(exitware(__dirname + "/./explicitPathToMyExitwares");

Then it's bound to res, so from ANY controller when you get a bad parameter or input you can report it to the user and log it with ease:

app.get("/item/:itemName", function(req, res){
    if(!req.params.itemName){
        var myError = new Error("Missing itemName");
        return res.badParameters(myError);
    }
    if(!req.body.hasOwnProperty.somethingRequired){
        var myError = new Error("Missing somethingRequired");
        myError.someMeta = {hah:"meh"};
        return res.badParameters(myError);
    };
    // All OK, do your code below... 
    
});

Further Examples

Check out the examples folder on GitHub

Keywords

FAQs

Package last updated on 06 Sep 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