How to write code for routing and route handler's using nodejs(without using frameworks line express/compound)

1,146 views
Skip to first unread message

venkat

unread,
Jul 1, 2014, 7:51:01 AM7/1/14
to nod...@googlegroups.com
I am very new to nodejs and I was searching examples which have code for routing and route handling but can't find. Can I write the code for route handler's without using express.js framework ?, Could you please provide the reference links for routing and routing handler examples using nodejs?

Nic Stage

unread,
Jul 1, 2014, 12:28:09 PM7/1/14
to nod...@googlegroups.com
I would suggest just taking a look at Express' code to see how they do it. Particularly the files in the /express/lib/ directory. It isn't a very big amount of code, actually.

zladuric

unread,
Jul 1, 2014, 11:34:07 PM7/1/14
to nod...@googlegroups.com

On Tuesday, July 1, 2014 1:51:01 PM UTC+2, venkat wrote:
I am very new to nodejs and I was searching examples which have code for routing and route handling but can't find. Can I write the code for route handler's without using express.js framework ?, Could you please provide the reference links for routing and routing handler examples using nodejs?

Like Nic suggested, take a look at Express/lib/router code. Also, take a look at connect:  https://github.com/senchalabs/connect/blob/master/lib/proto.js

But put most simply I can make it: your router.js module will register an array of valid {URL/HTTP method/handler function} sets, and when the Node's built-in 'http' module calls it for each request, it will match url and method to execute the appropriate handler.

But put simply, they all take req.pathname that the Node.js builtin 'http' module will provide for each request. It's the HTTP path sent by the browser.

There are two phases there - setup and later, usage. In the app setup, the app starts registering routes, you can think of it as a simple array for a simple server:

[
  {
    path: '/profile',
    method: 'GET',
    handler: [Function]
  }, {
    path: '/notes',
    method: 'GET',
    handler: [Function]
  }, {
    path: '/notes',
    method: 'POST',
    handler: [Function]
  }, {
    path: '/',
    method: 'GET',
    handler: [Function]
  }]

So when the app is set up, it now serves requests. Node's 'http' module will get each request (and that requests' response object, on which you can attach your reply) and give it to your router.
Your router will compare that request with the array. If it matches one, it will call it's handler, passing the request and response.

Does any of this make sense to you?

Kind of. At least I think that's the way it works, there are smarter people then me here who should do the teaching :)


-- 
Zlatko 

Ethan Garofolo

unread,
Jul 1, 2014, 11:53:04 PM7/1/14
to nod...@googlegroups.com
I did a screencast that gets into routing without Express: http://www.learnallthenodes.com/episodes/3-beginning-routing-in-nodejs

The conclusion of the episode was, "use Express," but I think it's useful to see the pain that Express solves.  It comes down to using the url and method properties on the http.incomingMessage objects that come into your handlers. (http://nodejs.org/api/http.html#http_http_incomingmessage).

Is this beyond where you are?  How far into setting up your server did you get?

Peter Rust

unread,
Jul 2, 2014, 1:01:32 PM7/2/14
to nod...@googlegroups.com
There's a very simple router here that's just a switch statement with regex patterns: http://www.idryman.org/blog/2013/08/16/minimal-nodejs-router/

Angel Java Lopez

unread,
Jul 2, 2014, 5:52:56 PM7/2/14
to nod...@googlegroups.com
A simple router
(no parameters, only routes)
It's an evolution of screencasts in Spanish


--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/18dbbbb7-2360-4be3-bbb5-a92a5f4380f0%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Leo Oshiro

unread,
Jul 3, 2014, 5:11:41 AM7/3/14
to nod...@googlegroups.com
I also wrote a router, I wanted to get into the different algorithms and paradigms. Heres my approach:

First i wrote a parser, the parser broke down the path into pieces, and looked for parameters and regex, it would then classify the route as static or dynamic. It would then add to a static routing hashtable or a more complex dynamic routing table.

The router/dispatcher would then start filtering and recursing into the routing table, until it found a handler. First it would filter by request method, then try a match on the static hashtable, or fallback to the dynamic routing table, breaking the url into pieces, recursing into the table, while extracting possible parameters from the url.

Read this src files:

I think my approach is overly complicated :(

In the end I agree with Ethan, in most cases just use express.


On Wednesday, July 2, 2014, Peter Rust <pe...@cornerstonenw.com> wrote:
There's a very simple router here that's just a switch statement with regex patterns: http://www.idryman.org/blog/2013/08/16/minimal-nodejs-router/

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages