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

dynamic-routes

v0.2.1

Published

Dynamic routes for Express.JS

Readme

dynamic-routes

A dynamic router / loader for http://expressjs.com/

Install

npm install dynamic-routes --save

Usage

DynamicRoutes(app, path_to_routes, [options])

Examples :

Express.JS Server :

var express = require('express'),
	http = require('http'),
	path = require('path'),
	DynamicRoutes = require('dynamic-routes'),
	app = express();

app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.use(express.logger('dev'));
app.use(bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('secrets secrets, we got extra'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

if ('development' == app.get('env')) {
	app.use(express.errorHandler());
}

DynamicRoutes(app, __dirname + '/routes/');
// or

/*
DynamicRoutes(app, {
	src: __dirname + '/routes/',
	ignore: [],
	pattern: /\.js$/
});
*/

http.createServer(app).listen(app.get('port'), function(){
	console.log('Express server listening on port ' + app.get('port'));
});

Creating compatible routes :

Using an object :
//routes/index.js
module.exports = {
	priority: 1000, //this is the `/` handler, therefore it should be the last route.
	path: '/',

	//this function gets passed the express object one time for any extra setup
	init: function(app) {
		
	},
    
    //a middleware that is called from any HTTP methods
    ALL: function(req, res, next) {
        res.send(' - Common Middleware -');
        next();
    }
    
    //HTTP method specific routes
	GET: function(req, res) {
		res.end('GET ');
	},
	
	POST: function(req, res) {
        res.json(req.body);
        res.end('POST ');
	},
    
    //routes supports GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE, PATCH method(all uppercase)
};
Routing multiple middlewares by assigning an array of middlewares :
module.exports = {
    priority: 1000,
    path: '/',
    
    init: function(app){
        
    }
    
    ALL: [
        function(req, res, next){
            res.send(' - Common Middleware 1 - ');
            next();
        },
        function(req, res, next){
            res.send(' - Common Middleware 2 - ');
            next();
        }
    ],
    
    GET: [
        function(req, res, next){
            res.send(' GET 1 ');
            next();
        },
        function(req, res){
            res.end('end of GET');
        }
    ],
    
    POST: [
        function(req, res){
            res.json(req.body);
            res.end('end of POST');
        }
    ]
    /* an array with single route, that is equivalent to
    
    POST: function(req, res){
        res.json(req.body);
        res.end('end of POST');
    }
    
    */
}
Using a function :
var user = module.exports = function(app) {
	"use strict";
	//other setup
	app.get(function(req, res) {
		res.json({'user': [req.params.id, req.params, req.query, req.body]});
	});
};

user.priority = 1;
user.path = '/user/:id?';
This is equivalent to :
module.exports = {
	path: '/user/:id?',
	priority: 1,
	init: function (app) {
		//setup db
	},
	
	GET: function(req, res) {
		res.json({'user': [req.params.id, req.params, req.query, req.body]});
	}
}

Notes

  • As of Express 4.x, it's recommended to use require('body-parser')() instead of express.bodyParser(), check this.