Git Product home page Git Product logo

route66's Introduction

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 (189 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.

route66's People

Stargazers

Eric Cornelius avatar  avatar Austris avatar Paul Day avatar Stephen Belanger avatar Akhil Kodali avatar Hugh Kennedy avatar Daniel Fagnan avatar A Gurha avatar Ken D: Uemura avatar tom zhou avatar  avatar Joohun, Maeng avatar Chee Aun avatar

Watchers

Vadim Demedes avatar James Cloos avatar  avatar

Forkers

hillct

route66's Issues

Critical error

Got an exception in route66.js when accesing a route.

Configuration

OS: Mac OS X Lion
Node: v0.6.12
node_modules:

├─┬ [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ └── [email protected] 
└─┬ [email protected] 
  └── [email protected]
App code
connect = require 'connect'
router = require 'route66'

app = connect()

router.get '/', (req, res) ->
    res.end()

app.use router
app.listen 8080
Output

curl http://localhost:8080

TypeError: string is not a function
    at String.CALL_NON_FUNCTION (native)
    at ./node_modules/route66/lib/route66.js:23:16
    at ./node_modules/route66/node_modules/async/lib/async.js:108:13
    at Object.forEachSeries (./node_modules/route66/node_modules/async/lib/async.js:124:9)
    at Object.handle (./node_modules/route66/lib/route66.js:22:20)
    at next (./node_modules/connect/lib/proto.js:180:15)
    at Function.handle (./node_modules/connect/lib/proto.js:188:3)
    at Server.app (./node_modules/connect/lib/connect.js:60:31)
    at Server.emit (events.js:70:17)
    at HTTPParser.onIncoming (http.js:1514:12)

Params with special characters

Hi Vadim, i saw that if i put a params like this-is-a-title as get request i receive an error. All goes well if i use normal text without other character.
Could you see if this is a Route66 problem?

Thanks for your time.
Best regards.

Strange problem with routing and params

Hi, in this commit of my app (https://github.com/anddimario/mucontent) appears a strange problem with routing. I've added the path:
router.get('/:staticpage?', utils.maintenance, utils.restricted, function(req,res) {....});
to manage static page. It seems that works but for some other get with params like: router.get('/page/:operation?' .....) it doesn't work. And the really strange thing is that if i put the code in different parts of the controller, different router.get() doesn't work.

I hope that you understand this very strange problem.

Could you see if this is a Route66 problem?

Thanks for your work.
Best regards

Don't process routing without params

Hi, in new version if there are params specification ( for example the routing: /page/:operation? ) routing works only if i send the params in the url. If i write only /page the system don't process the route.

Could you tell me if this is only my problem, or you find this issue too?

Thanks for your time.
Best regards.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.