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 🙏

© 2026 – Pkg Stats / Ryan Hefner

switchyard

v0.1.0

Published

Dynamic routing library for express.js applications.

Readme

switchyard

npm version Build Status

Dynamic routing library for express.js

The Problem

express.js makes development of RESTful applications a cinch. Unfortunately, it makes ugly application organization a cinch as well. How often have you seen single-page express.js applications that end up as route soup?

// bad-application.js
app.get( '/endpoint', function() { ... } );
app.post( '/endpoint', function() { ... } );
app.get( '/another-endpoint', function() { ... } );
app.patch( '/some-endpoint/:id', function() { ... } );

As applications grow, they sometimes become more organized. The tight coupling between routes and controllers remains, however; to locate the function that defines a route, you'll need to search the entire codebase.

Enter switchyard. switchyard performs dynamic routing based on controller structure. Your code now defines its routes implicitly, and simple separation of controllers is simple.

Installation

To install switchyard, just require it from npm:

npm install --save switchyard

Usage

Using switchyard is simple. Place your controllers in one director (we'll use controllers for our examples). A simple example server:

var app = require( 'express' )();
var switchyard = require( 'switchyard' );

// Starting switchyard is simple. Pass your express.js instance as the first
// parameter, and the full path to your controller directory as the second.
switchyard( app, __dirname + '/controllers' );

var server = app.listen( 3000, function() {
    console.log( 'A switchyard-powered express server is running on port 3000!' );
} );

Controller Syntax

Controllers are placed in your defined controller directory (the second parameter to switchyard). Their URL routes are generated by their name; for example, to create a controller that responds to endpoints beginning with /user, name your controller user.js. Controllers expose a single object that defines their behavior.

// controllers/user.js
module.exports = {
    // Endpoints that respond to /user/hello
    hello: {
        // GET /user/hello
        get: function( req, res ) {
            res.send( 'Hello, world!' );
        }
    }
};

These objects are two dimensional. The first dimension is the endpoint name (which will be reflected directly in the URL, as the comments above indicate). The second dimension controls the response to each HTTP verb, and returns a function that maps directly to an express.js route that defines the behavior. Every HTTP verb that express supports is supported here: GET, POST, PATCH, PUT, DELETE, etc.

Special endpoint names

If you'd like to map directly to the / route for a controller (for example, /user/), you can used the index route.

// controllers/test.js
module.exports = {
    // Endpoints that respond to /test/
    index: {
        // POST /test
        post: function( req, res ) {
            // ... do something
        }
    }
};

You can also use named routes, e.g. /user/view/:id.

// controllers/user.js
module.exports = {
    "view/:id": {
        get: function( req, res ) {
            var user_id = req.param( 'id' );
            res.send( 'Hello, user ' + user_id + '!' );
        }
    }
};

Route aliases

You can also define special routes, which don't match the controller name/method convention. To do this, pass a third parameter to the switchyard instantiation:

var route_aliases = {
    '/login': '/user/login',
    '/logout': '/user/logout'
};

switchyard( app, __dirname + '/controllers', route_aliases );

The key in the route_alias object is the alias URL you'd like to define, and the value is the path that it will map to. In this example, /login will now map to the login endpoint in controllers/user.js.

Aliases automatically map to every verb on the endpoint: for example, if controllers/user.js:login defined a GET and POST method, so would /login.

Multiple aliases may point to the same controller.

Coming Soon

I have a few future things I'd like to support that are coming soon:

  • Subdirectories in the controller directory

Contributing

Pull requests are gladly appreciated! You are also free to create issues for feature requests, bugs, or to complain about this documentation.