Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
super fast tiny router
JavaScript
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
.gitignore
Makefile
README.md
index.js
package.json
test.js

README.md

troute

super fast tiny router

install

$ npm install troute

usage

using troute is super simple

require('http').createServer(troute('GET', '/home', function (req, res) {
  res.end('+1');
})).listen(8000);

you can use variable assignment in your route and capture their values in the req.params object

require('http').createServer(troute('GET', '/videos/:id', function (req, res) {
    console.log(req.params.id); // '1234'
})).listen(5000);

api

troute(method, route, fn)

troute accepts a valid http method, a string or RegExp route, and a function fn(req, res, next) as a callback

  • method - a valid http method
  • route - a string or RegExp for route matching
  • fn - a callback function when the route is matched

see sregex for more information defining string and RegExp routes

example

require('http').createServer(troute('GET', '/home', function (req, res) {
  res.end('+1');
})).listen(8000);

.get(route, fn)

matches all GET requests

  • route - a string or RegExp for route matching
  • fn - a callback function when the route is matched

example

troute.get('/home', home);

.post(route, fn)

matches all POST requests

  • route - a string or RegExp for route matching
  • fn - a callback function when the route is matched

example

troute.post('/users', createUser);

.put(route, fn)

matches all PUT requests

  • route - a string or RegExp for route matching
  • fn - a callback function when the route is matched

example

troute.put('/videos/:id', editVideo);

.del(route, fn)

matches all DELETE requests

  • route - a string or RegExp for route matching
  • fn - a callback function when the route is matched

example

troute.del('/videos/:id', deleteVideo);

.head(route, fn)

matches all HEAD requests

  • route - a string or RegExp for route matching
  • fn - a callback function when the route is matched

example

troute.head('/resources', resourcesHead);

.options(route, fn)

matches all OPTIONS requests

  • route - a string or RegExp for route matching
  • fn - a callback function when the route is matched

example

troute.options('/videos', videosOptions);

license

MIT

Something went wrong with that request. Please try again.