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

canal

v1.1.2

Published

A powerful route design helper for Express.js web applications

Downloads

6

Readme

Canal

A structured routing toolkit for Express

Canal was designed to make structuring routes in Express as easy as possible by relying on JavaScript objects to design your routes. It allows you to easily manage complex routing strategies while ensuring that the code remains readable and navigatable.

Usage

Using Canal is extremely simple, just add a dependency to your package.json file or run npm install canal from the terminal.

Example

var express = require('express'),
	canal = require('canal');

var app = express();
app.use(express.bodyParser());
app.use(app.router);

var routes = {
	// GET /
	get: function(req, res) {
		return res.render('index');
	},

	login: {
		// GET /login
		get: function(req, res) {
			return res.render('login')
		},
		// POST /login
		post: function(req, res) {
			// login logic
			return res.redirect('/');
		}
	},
	user: [checkLogin, {
		// Translated into a placeholder
		$username: {
			get: function(req, res) {

			}
		}
	}]
};

canal(app, routes);

app.listen(process.env.port || 3000);

Features

  • Structured Route Design Canal allows you to design your routes by following a proven set of guides which ensure that your routes are easy to understand, extend and modify.
  • Tree Based Structure Your routes are tree-based, so why should you be forced to use a linear design approach? Embrace the power of JavaScript's object maps and realize just how easily complex routes can be designed and managed.
  • Powerful Syntax Canal allows for complex routing paradigms, including the use of Express' chained route handling while still keeping the syntax as simple as possible. Don't believe us? Try it out for yourself.

API

Canal's API consists of a single function which allows you to register routes with an application.

function Canal(app, routes, basePath = '/');

Routes Syntax

The routes used by Canal are formed by creating a nested JavaScript map object describing the different path branches. Each laef represents the path component at the depth of the branch it is linked to, so the path /user/login would be represented by the object { user: { login: {} } }. Each leaf can specify functions to handle different verbs (get, post, delete etc.).

Basic Routes

Basic routes are defined by creating an object like the following. Canal will attempt to register functions as route handlers, using their property name as the verb.

var routes = {
	get: function(req, res) { },

	user: {
		get: function(req, res) { },
		post: function(req, res) { }
	}
};

Placeholders

To further extend the power of routes, placeholders can be used, indicated by the presence of a $ sign before the name of the placeholder. These placeholders should be registered with Express using the app.param() function before they will work.

var routes = {
	get: function(req, res) { },

	$username: {
		get: function(req, res) { },
		post: function(req, res) { }
	}
};

Cascading Routes

Sometimes it's necessary to include request preprocessors on specific routes. For example, the ability to ensure that a user is signed in for any administrative routes is a common use case, and one that can be handled by a route preprocessor. Canal allows these preprocessors to be implemented very easily through the use of arrays as leaves in the route tree.

var routes = {
	get: function(req, res) { },

	admin: [ensureLogin, {
		get: function(req, res) { },
		pages: {
			get: [populatePages, function(req, res) { }]
		}
	}]
};

Cascading handlers are applied to all child routes when they appear with an Object as the last element, while they are implemented only for the given handler if the last element in the array is a function.