http - How do you use several request handler in node.js? -


i come tornado, have requesthandler class , match query orient right requesthandler.

how do in node.js? ????

http=require ('http'); url=require('url')   function case1(request,response){ ... }  function case2(request,response){ ... }  http.createserver(function(request, response) {      var q=url.parse(request.url, true).query      switch(true){        case /friend/.test(q):            case1(request,response);            return;        case /foes/.test(q):            case2(request,response);            return;      } }).listen(9999) 

with node.js' http.server, you're on own establishing routing.

and, you're close this. though, you'll want test based on parsed url's pathname rather query.

var pathname = url.parse(request.url, true).pathname;  switch (true) {     case /\/friend/.test(pathname):         case1(request, response);         break;      case /\/foes/.test(pathname):         case1(request, response);         break; } 

you'll want include testing request.method.

case request.method === 'get' && /\/friend/.test(pathname): 

or, soulcheck mentioned, there numerous libraries/framework available have established api routing, including express , restify.

var app = express();  app.get('/friend', case1); app.get('/foes', case2); 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

IIS->Tomcat Redirect: multiple worker with default -