A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/jonschlinkert/en-route below:

jonschlinkert/en-route: Routing for static site generators and build systems.

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.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.

Install with npm:

$ npm install --save en-route

en-route is a different, but similar concept to routes you might be familiar with, like express routes. The general idea is, you can:

  1. Use middleware to modify file objects
  2. Define routes, to determine whether or not a middleware function should run on a given file.
  3. Define handlers for running specific middleware at specific points in your application or build.

See the examples folder for a number of different examples of how en-route works.

const Router = require('en-route');
const router = new Router();

Create a new Router with the given options.

Params

Example

// initialize a router with handler methdods
const router = new Router({ handlers: ['preWrite', 'postWrite'] });

Register one or more middleware handler methods. Handler methods may also be added by passing an array of handler names to the constructor on the handlers option.

Params

Example

router.handlers(['onLoad', 'preRender']);

Register a middleware handler method.

Params

Example

router.handler('onLoad');

Create a new router instance with all handler methods bound to the given pattern.

Params

Example

const router = new Router({ handlers: ['before', 'after'] });
const file = { path: '/foo', content: '' };

router.route('/foo')
  .before(function(file) {
    file.content += 'foo';
  })
  .after(function(file) {
    file.content += 'bar';
  });

router.handle(file)
  .then(() => {
    assert.equal(file.content, 'foobar');
  });

Run a middleware methods on the given file.

Params

Example

// run a specific method
router.handle('onLoad', file)
  .then(file => console.log('File:', file))
  .catch(console.error);

// run multiple methods
router.handle('onLoad', file)
  .then(file => router.handle('preRender', file))
  .catch(console.error);

// run all methods
router.handle(file)
  .then(file => console.log('File:', file))
  .catch(console.error);

Runs all handler methods on the given file, in series.

Params

Example

router.all(file => {
  file.data.title = 'Home';
});

Mix router methods onto the given object.

Params

Example

const router = new Router();
const obj = {};
router.handlers(['before', 'after']);
router.mixin(obj);
console.log(obj.before) //=> [function]

Create a new Route with the given pattern, handler functions and options.

Params

Example

const fn = file => file.count++;
const Route = require('en-route').Route;
const route = new Route('/(.*)', [fn, fn, fn]);
const file = { path: '/foo', count: 0 };

route.handle(file)
  .then(file => {
    console.log(file.count); // 3
  });

Register one or more handler functions to be called on all layers on the route.

Params

Example

route.all(function(file) {
  file.data.title = 'Home';
});
route.all([
  function(file) {},
  function(file) {}
]);

Run a middleware stack on the given file.

Params

Example

route.handle(file)
  .then(file => console.log('File:', file))
  .catch(console.error);

Push a layer onto the stack for a middleware functions.

Params

Example

route.layer(/foo/, file => {
  // do stuff to file
  file.layout = 'default';
});

Push a layer onto the stack for one or more middleware functions.

Params

Example

route.layers(/foo/, function);
route.layers(/bar/, [function, function]);

Create a new Layer with the given pattern, handler function and options.

Params

Example

const layer = new Layer('/', file => {
  // do stuff to file
  file.extname = '.html';
});

Calls the layer handler on the given file if the file.path matches the layer pattern.

Params

Example

layer.handle(file)
  .then(() => console.log('Done:', file))
  .then(console.error)

Attempts to match a file path with the layer pattern. If the path matches, an object of params is returned (see path-to-regexp for details), otherwise null is returned.

Params

Example

const layer = new Layer('/:name');
console.log(layer.match('/foo')) //=> { name: 'foo' }

Breaking changes

Breaking changes

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

You might also be interested in these projects:

Brian Woodward

Jon Schlinkert

Copyright © 2018, Jon Schlinkert. Released under the MIT License.

This file was generated by verb-generate-readme, v0.8.0, on November 11, 2018.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4