首页 > node.js > 是否可以在React上运行守护进程?

是否可以在React上运行守护进程? (Is it possible to run a daemon on React?)

问题

我想构建一个React应用程序,其中应用程序从7个不同的API(即使没有人在线)持续全天拉动,并在收到给定值时发送自动消息。是否可以仅使用React或者需要Node.js?如果是这样你会怎么做?

解决方法

一些关键点:

  1. 在大多数情况下,JS是单线程。当你说守护进程时,我们通常会谈论一个单独的线程或进程。所以这对js来说是不可行的,特别是在浏览器中。但是,为了满足您的需求,您不必拥有守护进程。您只需在js代码中设置一些计时器,并在时间点发出请求。
  2. 如果您只想从前端调用7个不同的API,那么您可能会遇到跨域问题,除非这些API由其他人合法打开并且不会阻止跨域请求。
  3. 如果您需要时间非常准确,最好在后端调用API并缓存所需的数据。如果您想这样做,那么可能会使用nodejs,具体取决于您最熟悉的语言。我的建议是在后端进行。它更易于管理,您可以实现真正的多线程或多进程守护程序。

问题

I'd like to build a React application where the app continually pulls throughout the day from 7 different APIs (even when nobody is online) and sends automated messages when receiving a given value. Would it be possible to do this only with React or is Node.js required? If so how would you do it?

解决方法

Some key points:

  1. JS is single thread in most cases. When you say daemon, typically, we are talking about a separate thread or process. So this is not doable with js typically especially in browser. But, to achieve your needs, you don't have to have a daemon. You just need to setup some timer in your js code and fires the requests when it's the time.
  2. If you want to call 7 different APIs just from frontend, then you may encounter cross-domain issue unless these APIs are legally opened by others and they don't prevent cross domain request.
  3. If you need the time to be very accurate, it's better to call the APIs on backend and cache the data you need. If you want to do so, then nodejs may be used depending on what language you are most familiar with. My suggestion is to do it on backend. It's easier to manage and you can implement real multi-thread or multi-process daemon.
相似信息