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

JulienFr

I come from tornado, where you have a requestHandler class and you match the query to orient it to the right requestHandler.

How do you do in node.js? Something like that ????

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)
Jonathan Lonowski

With Node.js' http.Server, you're on your own for establishing any routing.

And, you're close to this. Though, you'll want to test based on the parsed URL's pathname rather than 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 probably also want to include testing the request.method.

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

Or, as soulcheck mentioned, there are numerous libraries/framework available that have an established API for routing, including express and restify.

var app = express();

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do you use MultipleChoiceField with a get request?

From Dev

In node.js if no response is received from an http request, how do you know?

From Dev

How do you set a callback on timeout for the node.js request library?

From Dev

In node.js if no response is received from an http request, how do you know?

From Dev

Can you use Node Inspector with the .coffee handler?

From Java

What is the purpose of Node.js module.exports and how do you use it?

From Dev

How do you force express on node.js in Azure Websites to use https?

From Dev

How do you use Node.js to stream an MP4 file with ffmpeg?

From Dev

How do you force express on node.js in Azure Websites to use https?

From Dev

how do you get current request's in aiohttp when it isn't a handler

From Dev

How to use Request js (Node js Module) pools

From Dev

Use node js domains in handler function

From Dev

How do you create custom asynchronous functions in node.js?

From Dev

how do you redirect user to another link in node.js?

From Dev

How do you fetch and parse xml in node.js?

From Dev

Node.js: How do you handle callbacks in a loop?

From Dev

How do you serve CSS in a node.js application

From Dev

How do you incorporate node.js into java tests?

From Dev

How do you remove a handler using a d3.js selector

From Dev

How do you return mongodb info to the browser inside a hapi.js route handler?

From Dev

How do I get Node.js request type?

From Dev

In node.js How do I get the protocol of a request object?

From Dev

How Do I Make a POST Request with Node.JS to Ghostbin?

From Dev

how to do somthing before an HTTP POST request in node.js?

From Dev

node.js request handler wont call a function

From Dev

How do you use headroom.js with Bootstrap 3 navbar?

From Dev

How do you use React.js for SEO?

From Dev

How do you remove a node?

From Dev

How to rewrite node js request

Related Related

  1. 1

    How do you use MultipleChoiceField with a get request?

  2. 2

    In node.js if no response is received from an http request, how do you know?

  3. 3

    How do you set a callback on timeout for the node.js request library?

  4. 4

    In node.js if no response is received from an http request, how do you know?

  5. 5

    Can you use Node Inspector with the .coffee handler?

  6. 6

    What is the purpose of Node.js module.exports and how do you use it?

  7. 7

    How do you force express on node.js in Azure Websites to use https?

  8. 8

    How do you use Node.js to stream an MP4 file with ffmpeg?

  9. 9

    How do you force express on node.js in Azure Websites to use https?

  10. 10

    how do you get current request's in aiohttp when it isn't a handler

  11. 11

    How to use Request js (Node js Module) pools

  12. 12

    Use node js domains in handler function

  13. 13

    How do you create custom asynchronous functions in node.js?

  14. 14

    how do you redirect user to another link in node.js?

  15. 15

    How do you fetch and parse xml in node.js?

  16. 16

    Node.js: How do you handle callbacks in a loop?

  17. 17

    How do you serve CSS in a node.js application

  18. 18

    How do you incorporate node.js into java tests?

  19. 19

    How do you remove a handler using a d3.js selector

  20. 20

    How do you return mongodb info to the browser inside a hapi.js route handler?

  21. 21

    How do I get Node.js request type?

  22. 22

    In node.js How do I get the protocol of a request object?

  23. 23

    How Do I Make a POST Request with Node.JS to Ghostbin?

  24. 24

    how to do somthing before an HTTP POST request in node.js?

  25. 25

    node.js request handler wont call a function

  26. 26

    How do you use headroom.js with Bootstrap 3 navbar?

  27. 27

    How do you use React.js for SEO?

  28. 28

    How do you remove a node?

  29. 29

    How to rewrite node js request

HotTag

Archive