http-router

router module for node.js

http-router

router module for node.js

$ npm install http-router
var http = require('http'),
    router = require('http-router'),
    routes = new router;
 
routes
  .get('/', function(reqresnext) {
    res.write('Hello,');
    return next();
  })
  .get('/', function(reqresnext) {
    res.write(' World');
    return next();
  })
  .get('/', function(reqresnext) {
    res.end('!\n');
  })
  .post('/', function(reqresnext) {
    res.write('PO');
    return next();
  })
  .post('/', function(reqresnext) {
    res.write('ST');
    return next();
  })
  .post('/', function(reqresnext) {
    res.end('!\n');
  });
 
http.createServer(function(reqres) {
  routes.route(req, res);
}).listen(3000);
$ curl -X GET http://localhost:3000
Hello, World!
$ curl -X POST http://localhost:3000
POST!
$ npm install
$ npm test
  • req http.ServerRequest - http server request object
  • res http.ServerResponse - http server response object

call added HTTP method callback functions.

get/post/put/delete/options/trace/connect/patch(path, cb)

  • path string - request path

  • cb function(req, res, next) - callback function

    • req http.ServerRequest - http server request object
    • res http.ServerResponse - http server response object
    • next function() - call next function
  • return HttpRouter - return this instance object.

add HTTP method callback function.

The MIT License. Please see LICENSE file.