3

With respect to how node.js fits in with clients and web servers, is my description below correct?

  • (A) are clients
  • (B) is node.js running on some web server
  • (C) are "services" hosting business logic, database access routines, e.g. "GetCustomer()". For simplicity assume that a service (C) exposes a REST interface.

So in the flow, the client (A) will request some resource from node.js (B) which will in turn dispatch this request (with all it's async and evented i/o goodness) to a service (C) which might go and get some customer information and returns it to node.js (B) via callback and then in turn node.js returns that response to the client.

1.Is this correct?

Two related questions:

2.How does node.js know which service to dispatch a request to? Do you have to create api "stubs" in node.js that mirror the service APIs, since the client isn't talking directly to the services?

3.How is session state handled in this architecture?

Howiecamp
  • 2,981
  • 6
  • 38
  • 59
  • Why can't it be handled by Node.js? You might do better asking on the Node.js mailing list. – Josh K Jan 25 '11 at 20:14
  • @Josh - Not sure what you mean by your first question. With regard to the mailing list, this is StackOverflow! There are tons of node.js questions here. The purpose of SO is to consolidate this stuff. – Howiecamp Jan 25 '11 at 20:59
  • The purpose is to ask direct questions, this is more philosophy and purpose of use as compared to *"How do I do X with Node.js?"*. – Josh K Jan 25 '11 at 21:06
  • @Josh - My question has nothing to do with philosophy or purpose of use whatsoever. It is a specific question about where node.js sits in the client/server communication process. As example, my first question is asking about the specific interaction between the client and node.js and back end services. That's a specific technical question. My second question is also a specific technical one. – Howiecamp Jan 26 '11 at 13:17
  • Node.js can fit in many places, which is why I say the question is general. – Josh K Jan 26 '11 at 13:48

2 Answers2

7

First of all a "diagram" of the usual flow:

     Client                                
       |                                    
       v                                    
     Request                               
       |                                  
       v                                                          
(load balancer e.g. nginx)                
       |                                  
       v                                    
 Node.js Instance                          
 |     |      |                             
 v     v      V                            
DB    APIS   FILES                         

Concerning your last two questions:

  1. How do you want it to know that? Node.js is a generic framework you'll have to write the code to handle this.

  2. Again, Node.js is completely generic. If you have only one instance you could do it in-memory. Otherwise you'd would likely use redis or the like.

You can write game servers in Node.js, you can just crunch numbers, or you write a web server.

But you don't have to, do it the way you like or search for a framework that does it the way you like.

Ivo Wetzel
  • 46,459
  • 16
  • 98
  • 112
  • I've realized that with respect to node I'm in the "unknown unknowns" category. I know so little that I don't even know what to ask. As I look at posts about node.js here and elsewhere I can see the same conclusion. Lots of people ask what it is and seem to be ambiguous on the answer because concrete example use cases seem hard to come by. About my question #2 I definitely asked it poorly. Can you give some very basic example of how an http request might made to node (let's say to some rest API .../customer/1) and subsequently dispatched to that rest service? – Howiecamp Jan 28 '11 at 15:52
  • In order to handle REST you'll have to set up a `httpServer` and then handle the incoming connections, there are a ton of modules out there that people build which may already do that sort of stuff for you (https://github.com/ry/node/wiki/Modules#web-frameworks-full) I myself are currently building a site with http://expressjs.com/ and I've also built a Multiplayer Game with Node.js last year http://github.com/BonsaiDen/NodeGame-Shooter. You should check out the links/talks in our Tag Wiki http://stackoverflow.com/tags/node.js/info – Ivo Wetzel Jan 28 '11 at 18:31
2

Node.js is a framework for writing applications in javascript that don't run in a web browser. Because of its async nature it happens to be really good at writing web services. For (B) Node.js is the webserver, it doesn't run inside a webserver (apache). For (C) all of your logic can just be in your Node.js app or, your Node.js app can talk to some other service to get data. It is totally up to you.

For 2, you can do it how ever you want. You are writing the code, do it the way that makes sense in your app.

For 3, state is handled by a session/connection object being passed around to the callbacks.

Zanson
  • 3,991
  • 25
  • 31