node package manager

en-route

en-route NPM version Build Status

Routing for static site generators, build systems and task runners, heavily based on express.js routes but works with file objects. Used by Assemble, Verb, and Template.

Install

Install with npm:

$ npm i en-route --save

API

Router

Initialize a new Router with the given options.

Params

  • {Object}: options
  • returns {Router}: which is an callable function

.param

Map the given param placeholder name(s) to the given callback.

Parameter mapping is used to provide pre-conditions to routes which use normalized placeholders. For example a :user_id parameter could automatically load a user's information from the database without any additional code, The callback uses the same signature as middleware, the only difference being that the value of the placeholder is passed, in this case the id

of the user. Once the next() function is invoked, just like middleware it will continue on to execute the route, or subsequent parameter functions.

Params

  • name {String}
  • fn {Function}
  • returns {Router} Object: for chaining

Example

app.param('user_id', function(file, next, id) {
  User.find(id, function(err, user) {
    if (err) {
      return next(err);
    } else if (!user) {
      return next(new Error('failed to load user'));
    }
    file.user = user;
    next();
  });
});

.use

Use the given middleware function, with optional path, defaulting to /.

The other difference is that route path is stripped and not visible to the handler function. The main effect of this feature is that mounted handlers can operate without any code changes regardless of the prefix pathname.

Params

  • fn {Function}

Example

var router = new Router();
 
router.use(function(file, next) {
  false.should.be.true;
  next();
});

.route

Create a new Route for the given path. Each route contains a separate middleware stack.

See the Route api documentation for details on adding handlers and middleware to routes.

Params

  • path {String}
  • returns {Object} Route: for chaining

.method

Add additional methods to the current router instance.

Params

  • methods {String|Array}: New methods to add to the router.
  • returns {Object}: the router to enable chaining

Example

var router = new Router();
router.method('post');
router.post('.hbs', function(file, next) {
  next();
});

.all

Add a handler for all methods to this route.

Behaves just like middleware and can respond or call next to continue processing. You can use multiple .all call to add multiple handlers.

Params

  • handler {Function}
  • returns {Object} Route: for chaining

Example

function checkSomething(file, next) {
  next();
};
 
function validateUser(file, next) {
  next();
};
 
route
  .all(validateUser)
  .all(checkSomething)
  .get(function(file, next) {
    file.data.message = "Hello, World!";
  });

Related

  • assemble: Static site generator for Grunt.js, Yeoman and Node.js. Used by Zurb Foundation, Zurb Ink, H5BP/Effeckt,… more | homepage
  • gulp-routes: Add middleware to run for specified routes in your gulp pipeline. | homepage
  • template: Render templates using any engine. Supports, layouts, pages, partials and custom template types. Use template… more | homepage
  • verb: Documentation generator for GitHub projects. Verb is extremely powerful, easy to use, and is used… more | homepage

Authors

Jon Schlinkert

Brian Woodward

License

Copyright © 2016 Jon Schlinkert Released under the MIT license.

Based on previous work by: Copyright (c) 2009-2014 TJ Holowaychuk tj@vision-media.ca Copyright (c) 2012-2013 Jared Hanson http://jaredhanson.net/


This file was generated by verb on January 01, 2016.