简体   繁体   English

Node JS + Express JS:从其他位置刷新页面

[英]Node JS + Express JS: refresh page from other location

I have the following problem: I want to change one variable on a page.我有以下问题:我想更改页面上的一个变量。 The input comes from another page so:输入来自另一个页面,因此:

I'm using Node.js, Express.js and Ejs for this task.我正在使用 Node.js、Express.js 和 Ejs 来完成这项任务。

  • Server - storing the values服务器 - 存储值
  • Index page - Control page with input fields and send button索引页 - 带有输入字段和发送按钮的控制页
  • Display page - Shows the variable显示页面 - 显示变量

I'm sending the variable with fetch post to the server.我将带有 fetch post 的变量发送到服务器。 On the server I change the variable with the request body value and when I reload the "Display page" manually I see the new value.在服务器上,我使用请求正文值更改变量,当我手动重新加载“显示页面”时,我会看到新值。 The problem is: I need to change it without any manual refresh or other things, because that won't be possible.问题是:我需要在没有任何手动刷新或其他事情的情况下更改它,因为这是不可能的。

There is the possibility with "location.reload()" to refresh it every X second. “location.reload()”有可能每 X 秒刷新一次。 But that's not the way I want to use, I really just want to refresh it when the variable changes.但这不是我想使用的方式,我真的只想在变量更改时刷新它。 Is there a function (from express.js for example) I can use for it?是否有我可以使用的函数(例如来自 express.js)?

edit: I should mention that this project would be just used in our network and its not open for other users.编辑:我应该提到这个项目只会在我们的网络中使用,它不会对其他用户开放。 Like an in-house company dashboard kind of.就像内部公司仪表板一样。 So a "quick and dirty" solution can work too, but I want to learn something and wanted to do it the right way though.所以“快速而肮脏”的解决方案也可以工作,但我想学习一些东西,并想以正确的方式去做。

The simplest solution that came to mind is to have the server return the updated post in the body, then use that to update the page.想到的最简单的解决方案是让服务器返回正文中更新的帖子,然后使用它来更新页面。

You can also read about long/short polling and Websockets.您还可以阅读有关长/短轮询和 Websockets 的信息。

This is a very common scenario that has several solutions:这是一个非常常见的场景,有几种解决方案:

  1. Polling - The display page runs ajax calls in a loop every N seconds asking the server for the lastest version of the variable.轮询-显示页面每N秒循环运行一次 ajax 调用,向服务器询问变量的最新版本。 This is simple to implement, is very common, and perfectly acceptable.这很容易实现,非常普遍,并且完全可以接受。 However, it is a little outdated, and there are more modern and efficient methods.但是,它有点过时了,并且有更现代和更有效的方法。 I suggest you try this first, and move on to others only as needed.我建议您先尝试一下,然后仅在需要时才转向其他人。
  2. WebSockets - WebSockets maintain a connection between the client and server. WebSockets - WebSockets 维护客户端和服务器之间的连接。 This allows the server to send messages to the client application if/when needed.这允许服务器在需要时向客户端应用程序发送消息。 These are a little more complex to setup than just plain ajax calls, but if you have a lot of messages getting sent back and forth they are much more efficient.这些设置比简单的 ajax 调用稍微复杂一些,但是如果您有很多消息来回发送,它们会更有效率。
  3. WebRTC - This is taking it to another level, and is certainly overkill for your use case. WebRTC - 这将它提升到另一个层次,对于您的用例来说肯定是矫枉过正。 WebRTC allows direct messaging between clients. WebRTC 允许客户端之间的直接消息传递。 It is more complicated to configure than WebSockets and is primarily intended for streaming audio or video between clients.它比 WebSockets 配置更复杂,主要用于在客户端之间传输音频或视频。 It can however send simple text messages as well.然而,它也可以发送简单的文本消息。 Technically, if you want to persist the message on the server, then this is not suitable at all, but it's worth a mention to give a complete picture of what's available.从技术上讲,如果您想将消息保留在服务器上,那么这根本不适合,但值得一提的是提供可用内容的完整图片。

One possible solution would be to add page reload code after a successful post-operation with fetch.一种可能的解决方案是在成功执行 fetch 后操作后添加页面重新加载代码。

fetch(url, {
    method: 'post',
    body: body
  }).then(function(response) {
    return response.json();
  }).then((data) => {
        // refresh page here
        window.location.replace(url);
  });

Proper solution (WebSockets):正确的解决方案(WebSockets):

  1. Add WebSocket server as a part of your Node.JS app添加 WebSocket 服务器作为您的 Node.JS 应用程序的一部分
  2. Implement subscriptions for the WebSocket, implement function 'state changed'.实现对 WebSocket 的订阅,实现功能“状态已更改”。
  3. subscribe on a method 'state changed' from your client browser app.从您的客户端浏览器应用程序订阅“状态已更改”的方法。
  4. call ws server from your express app to update the clients when your variable is changed当您的变量更改时,从您的 Express 应用程序调用 ws 服务器以更新客户端

Outdated (Polling):过时(轮询):

  1. Add express endpoint route: 'variable-state' Call server from your添加快速端点路由:'variable-state' 从您的呼叫服务器

  2. client every n ms and check whether variable state is changed.客户端每 n ms 并检查变量状态是否已更改。

  3. Refresh the page if variable is changed.如果变量更改,请刷新页面。

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

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