简体   繁体   English

node.js客户端/服务器同步架构

[英]node.js client/server synchronous architecture

I've been developing a cloud based server in node.js, and have really enjoyed working with server-side javascript. 我一直在使用node.js开发基于云的服务器,并且非常喜欢使用服务器端javascript。

I'm writing a mobile app next which will interact with the server. 接下来,我要编写一个将与服务器交互的移动应用程序。

My question is : How should the client/server interaction work when node.js forces you to program asynchronously? 我的问题是:当node.js强制您进行异步编程时,客户端/服务器交互应该如何工作?

For example, let's say the user wants to login, and a post request is sent to my node.js server - is it best practice to keep the connection open somehow whilst node does its thing? 例如,假设用户要登录,然后将发布请求发送到我的node.js服务器-最佳做法是在节点执行其操作时以某种方式保持连接打开吗? I'm unsure how an anonymous function in a callback for example would be able to send a response back (especially if the context is lost). 我不确定例如回调中的匿名函数将如何发送回响应(尤其是如果上下文丢失)。

OR would you have the server send back an 'OK (I'm working on it)' and then somehow push to the client when ready? 还是您让服务器发回“确定(我正在处理)”,然后在准备就绪时以某种方式推送到客户端?

Sorry for the generalities.. As you all know node likes to accept callback functions whenever waiting is involved (Eg/ database call), and I'm still new to this kind of working. 抱歉,笼统地说。.大家都知道,节点喜欢在涉及等待时(例如,数据库调用)接受回调函数,而我对这种工作方式还是陌生的。

Thanks! 谢谢!

These details are interesting only if you're implementing a HTTP server from scratch. 仅当您从头开始实现HTTP服务器时,这些详细信息才有意义。 If you're just looking to create an app which communicates with the server through GET/POST requests, just use the http core module or a framework like Express, you don't need to care about things like keeping the connection alive. 如果您只是想创建一个通过GET / POST请求与服务器通信的应用程序,则只需使用http核心模块或Express之类的框架,就无需担心保持连接活动的麻烦。

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

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