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

pitstop

v0.2.1

Published

An express middleware for creating conditional groups of other middleware.

Downloads

3

Readme

#pitstop.js

An express middleware for creating conditional groups of other middleware.

##Installation

NPM: npm install pitstop

##Usage

###pitstop([crew, [conditionFunction]])

Creates the pitstop. The initial middleware crew and run condition can be passed directly as arguments. Returns the pit middleware function object.

###pit.use(middleware)

Adds a middleware to the pit crew. Middleware can be added individually, as multiple arguments, or in an array.

pit.use(morgan());
pit.use(cookieParser(), bodyParser());
pit.use([
	csrf(), errorHandler()
]);

Error handlers added to the pit will only catch errors that occur within the pit. If no error handler is defined, errors will bubble up to Express.

###pit.condition(null|function|truthy)

Defines the condition for if the pit crew should run. If the passed value is undefined, null or a truthy value, the crew will always run. If the passed value is false, the pit will be bypassed.

If a function is provided, the function will be executed as a middleware and receive the request, response and callback arguments. Calling the callback with false or an error will bypass the pit. Calling without any value run the pit crew.

###pit.execute

pit.execute is a bypass middleware which will skip the condition and always run the pit crew.

##Example

The following example will only run session handling and user loading if a session cookie already exists on the request. Said cookie will only be created when the user logs into the site.

var express = require('express');
var pitstop = require('pitstop');
var passport = require('passport');
var flash = require('connect-flash');

var expressCookies = require('cookie-parser');
var expressSession = require("express-session");

// Note, some passport setup code has been omitted, as it is not relevant to the example.

var userSession = pitstop();
    .condition(function (req, res, next) {
        if (req.cookies.session) {
            next();
        } else {
            next(false);
        }
    })
    .use(expressSession({
	    key: 'session'
	}))
    .use(flash())
    .use(passport.initialize())
    .use(passport.session())
    .use(function (req, res, next) {
    	if (req.isAuthenticated()) {
    		res.locals.user = req.user;
    	}
    	next();
    });


var app = express();

app.use(expressCookies);
app.use(userSession);

app.post('/login', userSession.execute, function (req, res) {
	// This route will always create/load a session.

	passport.authenticate('local', {
		successRedirect: req.session && req.session.goingTo || '/profile',
		failureRedirect: "/login",
		failureFlash: true
	})(req, res);
});

app.get('/', function (req, res) {
	// This route will only load a session if a session cookie exists,
	// which was created when they logged in.
});

##Running Unit Tests

From inside the repository root, run npm install to install the test dependencies.

Run npm test to execute the complete test suite.