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-autoroute

v2.1.1

Published

Express Auto Route automatically load routes from a structured routes/ folder

Downloads

456

Readme

Dependency Status Build Status Code Climate

Express Autoroute

This helper library originally came about after about a year of slowly improving how we structure our NodeJS and ultimately me being fed up of having to continuously type out app.get('/blah', blah_handler) and wondering where different paths are being constructed.

I also subscribe to the idea of convention over configuration because we developers have enough to think about. Also for something like NodeJS where there are so many people trying it out for the first time and fewer people blogging about best practices, sometimes its better to just follow convention and learn good ways to structure your app and write re-usable code.

Installation

npm install express-autoroute

Usage

First things first you need to enable the autorouter and pass it your express app.

var autoroute = require('express-autoroute');
autoroute(app,options); //where app is an express app;

For express-autoroute to start working you need to have a routes/ folder that contains specially formatted javascript files that expose an autoroute object that contains the public route api. The best way to describe it is with an example.

Options

Passed as a JS object to the autoroute initialisation funciton

//defaults
autoroute(app, {
	throwErrors: false,
	logger: require('winston'), //autoroute requires winston internally if you don't pass an instance to it
	routesDir: 'routes'
})

throwErrors: Boolean will throw all errors found while loading routes

logger: winston instance as a default it will use the internal default winston object. If you wish to use custom settings then pass in an instance defined within your app.

routesDir: String sets the directory to search for autoroute files

routeFile: String allows loading of one file instead of a directory, useful for testing

Example autoroute File

This is the contents of a file routes/blacklists.js. Note that the name of the file has nothing to do with the resulting endpoint so you can structure your files however you like.

module.exports.autoroute = {
	get: {
		'/blacklists' : get_all,
		'/blacklists/:id' : get
	},
	post: {
		'/blacklists/:id' : update,
		'/blacklists' : create
	},
	delete: {
		'/blacklists/:id' : del
	}
};

function get_all(req, res){
	//do something
}

...

As you can see the file is exposing a structured object. This object will be used later to decide what endpoints to create for the express application and will ultimately result in the following commands being executed

app.get('/blacklists/', get_all);
app.get('/blacklists/:id', get_all);
app.post('/blacklists/:id', update);
app.post('/blacklists', create);
app.detete('/blacklists/:id' : del);

And thats it! A nicely declarative way to load your routes.

Folders

express-autoroute also supports routes being separated into folders. The folder name will be added as a prefix to the express endpoints. For example, if you use the same blacklists.js file from before but move it to the folder routes/api/blacklists.js the resulting express commands will look like this

app.get('/api/blacklists/', get_all);
app.get('/api/blacklists/:id', get_all);
app.post('/api/blacklists/:id', update);
app.post('/api/blacklists', create);
app.detete('/api/blacklists/:id' : del);

Middlewares

A nifty feature in express is the ability to have route specific middlewares, and if they are used correctly they can really reduce the amount of code you write and always keep things DRY. express-autoroute supports the use of route middlewares by just adding an array to the autoroute object. Here is a silly little example but at least it explains the idea:

module.exports.autoroute = {
	get: {
		'/blacklists' : [authentication, get_all]
	}
};

function get_all(req, res){
	//use the user object to get something
	var myBlacklists = db.getMyBlacklists(req.user.id)
	res.send(myBlacklists);
}

function authentication(req, res, next){
	//get the user object or whatever
	req.user = dbUser;
	next();
}

Licence

Copyright (c) 2013, Chris Manson [email protected], Stone Circle Design LTD [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.