javascriptasynchronousnode.jsserverside-javascriptevented-io

Where does node.js sit in the client <--> web server flow?


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

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?


Solution

  • 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.