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

shrinkroute

v0.3.3

Published

Easy named routes for Express.

Downloads

8

Readme

Shrinkroute

Build Status NPM version Dependency Status Coverage Status

Named and nested routes for Express 3+, with URL building support. Helps you in achieving DRY routes!

Easy as that:

var shrinkr = shrinkroute( app, {
    "user": {
        path: "/user/:id?",
        get: showOrListUsers,
        post: [ requireAuthentication, createUser ],
        put: [ requireAuthentication, updateUser ]
    }
});

// in your routes...
function createUser( req, res, next ) {
    User.create(..., function( err, userId ) {
        // redirects to /user/1 (or any other userId...)
        res.redirect( req.buildUrl( "user", { id: userId } ) );

        // if full URLs are needed, try below - redirects to http://foobar.com/user/1
        res.redirect( req.buildFullUrl( "user", { id: userId } ) );
    });
}

// or views...
<a href="<%= url( "user", { id: 1 }) %>">User profile</a>
<a href="<%= fullUrl( "user", { id: 1 }) %>">User profile</a>

ATTENTION: Version 0.3.0 automatically adds the middleware responsible for providing URL builders in the view. When upgrading from previous versions, remove app.use( shrinkr.middleware ); line.

Nested routes

Nested routes are separated by an character, which is by default . (you may customize it if you want). When you set nested routes, they'll inherit their parent's route. For example:

shrinkroute( app, {
    "admin": {
        path: "/admin"
    },
    "admin.users": {
        path: "/users"
    }
});

This will end up in a route named admin which map to /admin, and another route named admin.users which map to /admin/users.

URL Builders in the view and in the request

The following functions are automatically available to you in every route set by Shrinkroute:

  • req.buildUrl and res.locals.url - builds paths for a route. The same as using shrinkr.url().
  • req.buildFullUrl and res.locals.fullUrl - builds full URLs for a route. The same as using shrinkr.fullUrl().

Installation

Install Shrinkroute via NPM:

npm install shrinkroute

API

[new] shrinkroute( [app][, routes][, separator = "."] )

Returns a new instance of Shrinkroute. This is a shortcut for the following:

var shrinkr = shrinkroute();
shrinkr.app( app );
shrinkr.separator( separator );
shrinkr.route( routes );

.app( [app] )

Get or set the app of this Shrinkroute instance. If setting the app, the following things will be available from now on:

  • app.shrinkroute - the Shrinkroute instance
  • req.route.name - the name of the matched route

.route( name[, route] )

Get or set a route by its name. When setting the route, the route path must be passed as route.path.

.route( name, path, route )

Set a route.

.route( [routes] )

Get all routes or set various routes at once.

.separator( [separator] )

Get or set the routes namespace separator. Useful for when using nested routes.

.url( name[, params][, append] )

Build the URL for a route. If the route path has parameters in the Express style (:param), they must be passed as a object in the params argument:

shrinkr.url( "user", {
    id: 1
});

If they're not passed, the returned URL will be blank. However, if you mark it as optional (:param?), it'll not be required.

Parameters passed in the params object that are not defined in the route will be appended to the query string, unless the append argument is falsy.

shrinkr.url( "users", {
    name: "foo"
});

// => /users?name=foo

shrinkr.url( "users", {
    name: "foo"
}, false);

// => /users

.fullUrl( name[, params][, append] )

Builds full URLs for a given route that include protocol, host and port. Respects the same rules as .url().

req.buildFullUrl( "users", {
  name: "foo"
});

// => http://foobar.com/users?name=foo

Testing

Shrinkroute is tested with Mocha. Inside the project root, run:

npm install
npm test

This will do the job for you!

License

MIT