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 

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/bb3e7819-4927-496c-ad55-cfc8eec62b89%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to