简体   繁体   English

node.js在客户端< - > Web服务器流中的位置?

[英]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? 关于node.js如何适应客户端和Web服务器,下面我的描述是否正确?

  • (A) are clients (A)是客户
  • (B) is node.js running on some web server (B)是在某个Web服务器上运行的node.js.
  • (C) are "services" hosting business logic, database access routines, eg "GetCustomer()". (C)是托管业务逻辑,数据库访问例程的“服务”,例如“GetCustomer()”。 For simplicity assume that a service (C) exposes a REST interface. 为简单起见,假设服务(C)公开REST接口。

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. 因此,在流程中,客户端(A)将从node.js(B)请求一些资源,该资源将依次将此请求(具有所有异步和事件的i / o良好性)分派给可能去的服务(C)。获取一些客户信息并通过回调将其返回到node.js(B),然后node.js将该响应返回给客户端。

1.Is this correct? 这是对的吗?

Two related questions: 两个相关问题:

2.How does node.js know which service to dispatch a request to? 2. node.js如何知道将请求分派给哪个服务? 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? 你是否必须在node.js中创建镜像服务API的api“stubs”,因为客户端不直接与服务对话?

3.How is session state handled in this architecture? 3.如何在此架构中处理会话状态?

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. Node.js是一个通用框架,你必须编写代码来处理这个问题。

  2. Again, Node.js is completely generic. 同样,Node.js是完全通用的。 If you have only one instance you could do it in-memory. 如果您只有一个实例,则可以在内存中执行此操作。 Otherwise you'd would likely use redis or the like. 否则你可能会使用redis之类的东西。

You can write game servers in Node.js, you can just crunch numbers, or you write a web server. 您可以在Node.js中编写游戏服务器,只需处理数字,或编写Web服务器。

But you don't have to, do it the way you like or search for a framework that does it the way you like. 但是你没有必要,按你喜欢的方式去做,或者搜索一个按照你喜欢的方式去做的框架。

Node.js is a framework for writing applications in javascript that don't run in a web browser. Node.js是一个用于在javascript中编写不在Web浏览器中运行的应用程序的框架。 Because of its async nature it happens to be really good at writing web services. 由于它的异构性,它恰好擅长编写Web服务。 For (B) Node.js is the webserver, it doesn't run inside a webserver (apache). 对于(B)Node.js是网络服务器,它不在网络服务器(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. 对于(C)您的所有逻辑都可以在您的Node.js应用程序中,或者您的Node.js应用程序可以与其他服务进行通信以获取数据。 It is totally up to you. 这完全取决于你。

For 2, you can do it how ever you want. 2,你可以随心所欲地做到这一点。 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. 对于3,状态由传递给回调的会话/连接对象处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM