npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-exitware

v1.1.0

Published

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

Downloads

4

Readme

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