Code Monkey home page Code Monkey logo

iroute's Introduction

iroute(fast and simple nodejs http/https router module) Build Status

if you only want to build a simple http server and don't want to use express,so iroute module could simple and fast to handler the request to action. the module are also be used in flat.js framework.

Installing the module

With npm:

iroute module is supported windows, linux, mac.

Make sure, node-gyp has installed.

 npm install iroute

From source:

 git clone https://github.com/DoubleSpout/iroute.git
 cd iroute
 node-gyp rebuild

To include the module in your project:

 var iroute = require('iroute');

##benchmark

to run 1000 handler and match 10000 times,regexp match and iroute result is follow:

  regexp: 1007ms
  cb1 run times :10000
  iroute: 251ms
  cb2 run times :10000

wow!!it's more than 3 times faster than regex match router.

example

var iroute = require('iroute')

iroute.add([

  ["get:/hello/world",function(req,res){res.end('hello world')}],

],function(req,res){

    res.statusCode = 404;

    res.end('404')

})

var http = require('http');

http.createServer(function (req, res) {

  iroute.route(req,res);

}).listen(8124);

then request the 127.0.0.1:8124 you can see 'hello world'

##API

iroute.add(routearray [,not_match_function]);

iroute.route(req,res); if iroute.route match the request,it will return 1,else return 0;

routearray:

[ [{method}:{url}?{key1}&{key2}, middle_function1, middle_function2, .., handle_function], ... ]

example:

  [

    ["get:/hello/world/?key1&key2", function middle1(req,res,ir_next){
      req.ir_count = 0;
      req.ir_count++;
      ir_next()},
    function middle2(req,res,ir_next){ req.ir_count++; process.nextTick(function(){ ir_next() }) },
    function middle3(req,res,ir_next){ req.ir_count++; setTimeout(function(){ ir_next(); },2000) }, //Asynchronous is ok
    function middle4(req,res,ir_next){ req.ir_count++; ir_next()},
    function handle(req,res){
      res.end( req.ir_count+''); //  req.ir_count will be 4!
    }]

  ]

ir_next([error]): if you don't call the function ir_next(),then will hang up the request,never responsed the rquest.so if you want to interrupt the routing, run the res.end() and never call the ir_next() or call the ir_next("some error") handle the request to the not_match_function; ir_next() function has an optional param error,if ir_next("some error");then will break the routing and call the not_match_function .it then setting the req.iroute_error = "some error";

not_match_function: if iroute not match the request,the not_match_function will be called.It has two parameters,req and res.

support: get,post,put,delete,options,head,other (other include copy,trace,lock.. etc)

more example see /example/example.js

##expressjs example

  var express = require('express');
  var app = express();
  var iroute = require("iroute");

  var route_array = [
    ["get:/hello/world",function(req,res){res.end('hello world')}],
  ]

  app.use(iroute.connect(route_array));

  app.listen(3000);

##benchmark to express and express+iroute

  express only(1000 routes)
  ab -c 100 -n 20000 http://192.168.28.5:8127/test/
  Requests per second:    1271.83 [#/sec] (mean)

  ab -c 500 -n 20000 http://192.168.28.5:8127/test/
  Requests per second:    1237.43 [#/sec] (mean)

  ab -c 800 -n 20000 http://192.168.28.5:8127/test/
  Requests per second:    1191.17 [#/sec] (mean)



  express+iroute (1000 routes)
  ab -c 100 -n 20000 http://192.168.28.5:8128/test/
  Requests per second:    1886.01 [#/sec] (mean)

  ab -c 500 -n 20000 http://192.168.28.5:8128/test/
  Requests per second:    1773.27 [#/sec] (mean)

  ab -c 800 -n 20000 http://192.168.28.5:8128/test/
  Requests per second:    1829.89 [#/sec] (mean)

iroute's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

iroute's Issues

build error on v0.11.13

make: Entering directory /var/www/wwwroot/node/express/demo/node_modules/iroute/build' CXX(target) Release/obj.target/iroute/src/iroute.o In file included from ../src/iroute.cpp:2: ../src/route.h:13: 错误:ISO C++ 不允许声明无类型的‘Arguments’ ../src/route.h:13: 错误:expected ‘,’ or ‘...’ before ‘&’ token ../src/route.h:14: 错误:ISO C++ 不允许声明无类型的‘Arguments’ ../src/route.h:14: 错误:expected ‘,’ or ‘...’ before ‘&’ token /root/.node-gyp/0.11.13/deps/v8/include/v8.h: In function ‘void Init(v8::Handle<v8::Object>)’: /root/.node-gyp/0.11.13/deps/v8/include/v8.h:845: 错误:‘v8::HandleScope::HandleScope()’是保护的 ../src/iroute.cpp:10: 错误:在此上下文中 ../src/iroute.cpp:12: 错误:‘NewSymbol’不是‘v8::String’的成员 ../src/iroute.cpp:13: 错误:对‘v8::FunctionTemplate::New(v8::Handle<v8::Value> (&)(int))’的调用没有匹配的函数 /root/.node-gyp/0.11.13/deps/v8/include/v8.h:3519: 附注:备选为: static v8::Local<v8::FunctionTemplate> v8::FunctionTemplate::New(v8::Isolate*, void (*)(const v8::FunctionCallbackInfo<v8::Value>&), v8::Handle<v8::Value>, v8::Handle<v8::Signature>, int) ../src/iroute.cpp:15: 错误:‘NewSymbol’不是‘v8::String’的成员 ../src/iroute.cpp:16: 错误:对‘v8::FunctionTemplate::New(v8::Handle<v8::Value> (&)(int))’的调用没有匹配的函数 /root/.node-gyp/0.11.13/deps/v8/include/v8.h:3519: 附注:备选为: static v8::Local<v8::FunctionTemplate> v8::FunctionTemplate::New(v8::Isolate*, void (*)(const v8::FunctionCallbackInfo<v8::Value>&), v8::Handle<v8::Value>, v8::Handle<v8::Signature>, int) make: *** [Release/obj.target/iroute/src/iroute.o] 错误 1 make: Leaving directory/var/www/wwwroot/node/express/demo/node_modules/iroute/build'
gyp ERR! build error
gyp ERR! stack Error: make failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:267:23)
gyp ERR! stack at ChildProcess.EventEmitter.emit (events.js:110:17)
gyp ERR! stack at Process.ChildProcess._handle.onexit (child_process.js:1046:12)
gyp ERR! System Linux 2.6.32-358.2.1.el6.x86_64
gyp ERR! command "node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /var/www/wwwroot/node/express/demo/node_modules/iroute
gyp ERR! node -v v0.11.13
gyp ERR! node-gyp -v v0.13.0
gyp ERR! not ok
npm ERR! [email protected] install: node-gyp rebuild
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] install script.
npm ERR! This is most likely a problem with the iroute package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node-gyp rebuild
npm ERR! You can get their info via:
npm ERR! npm owner ls iroute
npm ERR! There is likely additional logging output above.

npm ERR! System Linux 2.6.32-358.2.1.el6.x86_64
npm ERR! command "/usr/local/bin/node" "/usr/local/bin/npm" "install" "iroute"
npm ERR! cwd /var/www/wwwroot/node/express/demo
npm ERR! node -v v0.11.13
npm ERR! npm -v 1.4.9
npm ERR! code ELIFECYCLE
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /var/www/wwwroot/node/express/demo/npm-debug.log
npm ERR! not ok code 0

A small error in README...

var iroute = require('iroute')

iroute.add([
  ["get:/hello/world",function(req,res){res.end('hello world')}],
],function(req,res){
    res.statusCode = 404;
    res.end('404')
})

var http = require('http');
http.createServer(function (req, res) {
  iroute.route(req,res);
}).listen(8124);

"then request the 127.0.0.1:8124 you can see 'hello world'"

it should be "then request the 127.0.0.1:8124/hello/world you can see 'hello world'"

1

I want to know how to install iroute. I want to know the process.

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.