简体   繁体   English

仅在NodeJs和Express上通过http阻止请求构建网站

[英]Building Websites only on NodeJs and Express blocking requests over http

I have a question regarding the examples out there when using Nodejs, Express and Jade for templates. 我对使用Nodejs,Express和Jade作为模板时的示例有疑问。

All the examples show how to build some sort of a user administrative interface where you can add user profiles, delete them and manage them. 所有示例都展示了如何构建某种用户管理界面,您可以在其中添加,删除和管理用户配置文件。

Those are considered beginner's guides to NodeJs. 这些被认为是NodeJ的初学者指南。 My question is around the fact that if I have have 10 users concurrently accessing the same interface and doing the same operations, surely NodeJs will block the requests for the other users as they are running on the same port. 我的问题是,如果我有10个用户同时访问相同的界面并执行相同的操作,那么NodeJ肯定会阻止其他用户在同一端口上运行时对他们的请求。

So let's say I am pulling out a list of users which may be something like 10000. Yes I can do paging, but that is not the point. 因此,假设我要提取的用户列表可能类似于10000。是的,我可以进行分页,但这不是重点。 While I am getting the list from the server another 4 users want to access the application. 当我从服务器获取列表时,另外4个用户想要访问该应用程序。 They have to wait for my process to end. 他们必须等待我的过程结束。 That is my question - how can one avoid that using NodeJS & Express? 这就是我的问题-如何使用NodeJS&Express避免这种情况?

I am on this issue for a couple of months! 我在这个问题上待了几个月! I currently have something in place that does the following: 我目前有一些可以执行以下操作的内容:

  1. Run the main processing of stuff on a port 在端口上运行物料的主要处理
  2. Run a Socket.io process on a different port 在其他端口上运行Socket.io进程
  3. Use a sticky session 使用粘性会话
  4. The idea is that I do a request (like getting a list of items), and immediately respond with some request reference but without the requested items, thus releasing the port. 我的想法是我执行一个请求(例如获取项目列表),并立即以一些请求引用进行响应,但没有被请求的项目,因此释放了端口。
  5. In the background "asynchronously" I then do the process of getting the items. 然后在后台“异步”执行获取项目的过程。 Upon which when completed, I do an http request from one node to the socket node port node SENDING the items through. 完成后,我从一个节点向套接字节点端口节点发出Http请求,以通过这些端口发送项。
  6. When that is done I then perform a socket.io emit WITH the data and the initial request reference so that the correct user gets the message. 完成后,我然后执行一个socket.io发出数据和初始请求引用,以便正确的用户获取消息。
  7. On the client side I have an event listening for the socket which then completes the ajax request by populating the list. 在客户端,我有一个事件监听套接字,然后通过填充列表来完成ajax请求。

I have SOME success in doing this! 我在此方面取得了一些成功! It actually works to a degree! 它实际上在一定程度上有效! I have an issue online which complicates matters due to ip addresses, and socket.io playing funny. 我在线上有个问题,由于IP地址和socket.io玩得很滑稽,这使事情变得更加复杂。

I also have multiple workers using clustering. 我也有多个使用集群的工人。 I use it in the following manner: 我以以下方式使用它:

  1. I create a master worker 我创建一个主工人
  2. I spawn workers 我催生工人
  3. I take any connection request and pass it to the relevant worker. 我接受任何连接请求,并将其传递给相关的工作人员。

I do that for the main node request as well as for the socket requests. 我对主节点请求和套接字请求都这样做。 Like I said I use 2 ports! 就像我说的我使用2个端口!

As you can see I have had a lot of work done on this and I am not getting a proper solution! 如您所见,我为此做了很多工作,而我却没有找到合适的解决方案!

My question is this - have I gone all around the world 10 times only to have missed something simple? 我的问题是-我是否曾环游世界10次而错过了一些简单的事情? This sounds way to complicated to achieve a non-blocking nodejs only website. 这听起来很复杂,以实现一个非阻塞的nodejs only网站。

I asked myself - surely all these tutorials would have not missed on something as important as this! 我问自己-当然,所有这些教程都不会错过如此重要的事情! But they did! 但是他们做到了!

I have researched, read, and tested a lot of code - this is my very first time I ask anything on stackoverflow! 我已经研究,阅读和测试了很多代码-这是我第一次对stackoverflow提出任何问题!

Thank you for any assistance. 感谢您的协助。

PS One example of the same approach is this: I request a report using jasper, I pass parameters, and with the "delayed ajax response" approach as described above I simply release the port, and in the background a very intensive report is being generated (and this can be very intensive process as a lot of calculations are being performed)..! PS相同方法的一个示例是:我使用jasper请求报告,传递参数,并使用如上所述的“延迟ajax响应”方法,我只是释放了端口,并且在后台生成了非常密集的报告(这可能是一个非常密集的过程,因为正在执行大量计算)。 I really don't see a better approach - any help will be super appreciated! 我真的没有更好的方法-任何帮助将不胜感激!

Thank you for taking the time to read! 感谢您抽出宝贵的时间阅读!

I'm sorry to say it, but yes, you have been going around the world 10 times only to have been missing something simple. 我很遗憾地说,但是,是的,您已经走遍了世界十次,而只是错过了一些简单的东西。

It's obvious that your previous knowledge/experience with webservers are from a blocking point of view, and if this was the case, your concerns had been valid. 显然,您以前对Web服务器的了解/经验是从阻塞的角度来看的,如果是这种情况,则您的担忧是正确的。

Node.js is a framework focused around using a single thread to execute code, which means if it does any blocking operations, no one else would be able to get anything done. Node.js是一个专注于使用单个线程执行代码的框架,这意味着如果它执行任何阻塞操作,其他任何人都将无法完成任何事情。

There are some operations that can do this in node, like reading/writing to disk. 节点中可以执行某些操作,例如读/写磁盘。 However, most node operations will be asynchronous. 但是,大多数节点操作将是异步的。

I believe you are familiar with the term, so I won't go into details. 我相信您对这个词很熟悉,因此我不会赘述。 What asynchronous operations allows node to do, is to keep this single thread idle as much as possible. 异步操作允许节点执行的操作是,尽可能使此单个线程保持空闲状态。 By idle I mean open for other work. 闲置是指愿意从事其他工作。 If your code is fully asynchronous, then handling 4 concurrent users (or even 400) shouldn't be a problem, even for a single thread. 如果您的代码是完全异步的,那么即使对于单个线程,处理4个并发用户(甚至400个)也不应该成为问题。

Now, in regards to your initial problem of ports: Once a request is received on a given port, node.js execute whatever code you have written for it, until it encounters an asynchronous operation as soon as that happens, it is available to to pick up more requests on the same port. 现在,关于您最初的端口问题:在给定端口上收到请求后,node.js将执行您为该端口编写的任何代码,直到它遇到异步操作后,即可用于在同一端口上接收更多请求。

The second problem you inquire about, is the database operation. 您要查询的第二个问题是数据库操作。 In this case, node-js would send the query to the database (which takes no time at all) and the database does that actual execution of the query. 在这种情况下,node-js会将查询发送到数据库(这完全不需要时间),而数据库将执行查询的实际执行。 In the meantime, node is free to do whatever it wants, until the database is finished, and lets node know there is a result to fetch. 同时,节点可以自由地做任何想要的事情,直到数据库完成为止,并让节点知道要提取的结果。

You can recognize async operations by their structure: my_function(..., ..., callback). 您可以通过其结构识别异步操作:my_function(...,...,callback)。 Function that uses a callback function, is in most cases asynch. 在大多数情况下,使用回调函数的函数是异步的。

So bottom line: Don't worry about the problems around blocking IO, as you will hardly encounter any in node. 因此,最重要的是:不要担心阻塞IO的问题,因为您几乎不会遇到任何in节点。 Use a single port if you want (By creating multiple child processes, you can even have multiple node instances on the same port). 如果需要,请使用单个端口(通过创建多个子进程,您甚至可以在同一端口上具有多个节点实例)。

Hope this explains it good enough. 希望这足以说明问题。 If you have any further questions, let me know :) 如果您还有其他问题,请告诉我:)

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

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