node.js - 如何刷新车把视图中的数据?
问题描述
我正在开发一个项目,其中一部分向用户显示通知。我有一个用于存储通知的对象,以及一个显示通知的表格。通知以适当的格式写入 JSON 文件以供以后使用。
目前的情况是,该对象每 x 秒都会向其推送新通知,但实际表仅在页面重新加载时显示新消息。
我在寻找:
一种每隔 x 秒以编程方式刷新表中维护的数据的方法。
或者:我可以使用 webhook 或套接字之类的方法将新对象直接发送到客户端。
- 一种每 x 秒刷新一次网页的方法,不使用像 nodemon 这样的模块,它实际上完全杀死并重置了我的服务器。
JSON 文件
[
{
"Notification": "Limit Exceeded. Event has occurred. Action needed.",
"Time": "19:54:34",
"Date": "Sun Oct 28 2018 "
},
{
"Notification": "Limit Exceeded. Event has occurred. Action needed.",
"Time": "19:54:34",
"Date": "Sun Oct 28 2018 "
}
]
JSON 文件开始为空,并记录了通知。通知从这里提取,并存储在一个空的通知对象中。
车把视图。
<h2 class="page-header">Notifications</h2>
<h2>{{log}}</h2>
<p>Your Latest Notifications can be Found Below.</p>
{{#if notifications}}
<table style="width:100%" border="1">
<tr>
<th>Notification:</th>
<th>Time:</th>
<th>Date:</th>
</tr>
{{#each notifications}}
<tr>
<td>{{[Notification]}}</td>
<td>{{Time}}</td>
<td>{{Date}}</td>
</tr>
{{/each}}
</table>
{{else}}
<h3>No Current Notifications</h3>
{{/if}}
<form method="post" action="/test/notify" id="removeuserform">
<button type="submit" class="btn btn-default">Reload page.</button>
</form>
当然,页面是使用以下行呈现的。
res.render('home', {notifications: notifications});
解决方案?
我在这里研究了很多方法,也很想看看你的意见。我见过人们使用 webhook,我见过人们初始化一个按钮点击,它重定向到同一页面。我不能使用 window.reload 功能。我见过人们在 EJS 视图中放置标签,但我使用的是 Handlebars。
当我将新项目推送到对象中时,有没有办法简单地刷新此通知对象中的数据?非常感谢您的任何想法。先感谢您。
解决方案
推荐阅读
- javascript - 在对象的对象数组中设置相同的键
- node.js - “ORA-00972:标识符太长”使用 Node.js 插入 Oracle BLOB
- netlify - FetchError:网络超时:https://db.fauna.com/
- ruby-on-rails - Rails simple_form_for 更新操作路由错误 [POST] with id
- azure - 在 Azure Runbook 中轻松搜索作业
- laravel - 拉拉贡 |
- python - Python meshgrid 形状错误
- macos - Windows Net 表单未在 Visual Studio for mac 2019 上打开
- c# - 使用外部事件对 C# Azure 持久函数进行单元测试
- colors - gnuplot中最高点的颜色线