troute

super fast tiny router

troute

super fast tiny router

$ npm install troute

using troute is super simple

require('http').createServer(troute('GET', '/home', function (reqres) {
  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 (reqres) {
    console.log(req.params.id); // '1234' 
})).listen(5000);

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 (reqres) {
  res.end('+1');
})).listen(8000);

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);

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);

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);

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);

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);

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);

MIT