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-simple-router

v0.1.0

Published

Connects routes, from an object, to controller methods

Downloads

24

Readme

This module offers an Express 4 middleware that augments the built-in router. It supports named routes and offers a helper function for getting the url for a named route, which is very useful for templating. It allows you to define a set of handlers (controllers) for a set of routes in a more convential, MVC-style format.

Usage

//Define an array of routes, each in the format described below, e.g.:
var routes = [{ 
    path: "/test", 
    handler: "Test.index", 
    name: "test" 
}, { 
    path: "/test2", 
    handler: function(req, res, next) { 
        res.send('handler can be a function too!'); 
    }, 
    name: "test2"
}]; 

//Define an object of controllers (i.e. route handlers). 
//Each key is the name of a controller, as used by the router.
//The controller methods are automatically called with the urlFor
//function as their fourth argument, so you can do.,
// res.redirect(urlFor("test")) instead of hardcoding the redirect path.
var controllers = {
	Test: { 
		index: function(req, res, next, urlFor) { 
			res.send("index!"); 
		} 
	}
}; 

//Use the module.
//The second argument is the host for your app, used in url generation
var app = require("express")();
var simplerRouter = require("express-simple-router")(routes, "your-project-hostname.com");

app.use(simplerRouter.getRouter(controllers));

simplerRouter.urlFor("test"); //returns ‘/test’
simplerRouter.routes; //gives back the routes object

That’s it! Now a GET request to /test would return a page saying “index!”.

Route format

Each route is an object with the following keys.

  • path: the path to match, in the format used by the Express Router
  • handler: an express middleware function, or (as in the example above) a string corresponding to a controller method that should be used to handle the route. E.g. “Event.list”, where “Event” would be the name of the controller and “list” the name of the method. Using a string is convenient if you want to define your routes in a JSON configuration file or similar.
  • name (optional): a name for the route. Used by the urlFor method generate a url to that route.
  • method (optional, defaults to “get”): the HTTP method associated with this route; if the request method doesn’t match the one provided here, the controller won’t be called. Any value allowed by the Express router can be used here.