Latest Threat Research:Malicious dYdX Packages Published to npm and PyPI After Maintainer Compromise.Details
Socket
Book a DemoInstallSign in
Socket

route66

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

route66

Rails-like router for Koa and Express

Source
npmnpm
Version
0.3.1
Version published
Weekly downloads
16
128.57%
Maintainers
1
Weekly downloads
 
Created
Source

Route66

Route66 is a router middleware inspired by Rails for Koa and Express (and any other app or framework).

How is it different?

It is designed to suit medium and big Node.js apps, with ability to customize the way requests are dispatched.
Route66 provides a great API for readable and comfortable definitions of routes. Route66 can adapt to every project structure out there by letting you decide how request should be handled.

To better understand the idea behind this project, check out this example:

var Router = require('route66');
var express = require('express');

var router = new Router();
var app = express();

router.setup(function () {
  this.get('/posts/create', 'posts#create');
});

router.dispatch(function (route, req, res) {
  var controllerName = route.controller; // "posts"
  var methodName = route.method; // "create"
  
  // require PostsController and execute "create" method
  PostsController.create(req, res);
});

app.use(router.express());

Features

  • Flexible (allows you to handle the request the way you want)
  • Lightweight (165 sloc, commented and understandable code)
  • Convenient API for defining routes
  • Compatible with Koa and Express (and any other custom framework or app)
  • Extensive tests
  • Support for route parameters

Very soon:

  • Support regular expressions

Installation

Install via npm:

$ npm install route66 --save

All-in-one example

Website with a full documentation will be created soon.

var Router = require('route66');

var router = new Router();

router.setup(function () {
  this.root('home#index');
  
  this.get('/welcome', 'home#welcome');
  
  this.post('/contact', 'contact#send');
  
  this.resource('task');
  
  this.resource('user', { except: ['destroy'] });
  
  this.resource('post', function () {
    this.resource('comment', { only: ['create'] });
  });
  
  this.namespace('api', function () {
    this.resource('task');
  });
});

Dispatching requests in Express apps

var Router = require('route66');
var express = require('express');

var router = new Router();
var app = express();

router.setup(function () {
  this.namespace('api/v1', function () {
    this.post('/posts', 'posts#create');
  });
});

router.dispatch(function (route, req, res) {
  /*
  route = {
    controller: 'posts',
    method: 'create',
    namespace: 'api/v1'
  }
  */
});

app.use(router.express());

Dispatching requests in Koa apps

var Router = require('route66');
var koa = require('koa');

var router = new Router();
var app = koa();

router.setup(function () {
  this.namespace('api/v1', function () {
    this.post('/posts', 'posts#create');
  });
});

router.dispatch(function *(route, context) {
  /*
  route = {
    controller: 'posts',
    method: 'create',
    namespace: 'api/v1'
  }
  */
  
  // this is also context
  // this == context
});

app.use(router.koa());

Dispatching requests in custom apps

Use .resolve(method, url) to get a correct route for this request:

function handler (req, res) {
  let { route, params } = router.resolve(req.method, req.url);
  
  // custom handling
}

Tests

Circle CI

Note: You must have Node.js v0.11.x installed.

To run tests, execute this:

$ npm test

License

Route66 is released under the MIT license.

FAQs

Package last updated on 16 Jan 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts