简体   繁体   English

Node.js / Express.js实时显示文件更新

[英]Node.js / Express.js Display File Updates in RealTime

I wanted to know if there was a module in node that was could be use the Express module to display updated information in the browser from the contents of a file. 我想知道节点中是否有一个模块,可以使用Express模块​​从文件的内容在浏览器中显示更新的信息。

I have a .csv file that is updated and I would like to display the updates in real time over the express page. 我有一个已更新的.csv文件,我想在快递页面上实时显示更新。

Is there anything that can do this? 有什么可以做到的吗? Would Meteor.js be a better alternative? Meteor.js会是更好的选择吗?

Thanks. 谢谢。

What you need is just a persistent connection, with the possibility for the server to "push" data to client, whenever the file change. 您只需要一个持久连接,只要文件更改,服务器就可以将数据“推送”到客户端。

  • To push data, Socket.io should do the job. 要推送数据, Socket.io应该完成这项工作。 It use Websockets to maintain persistent connection. 它使用Websockets维护持久连接。 You just emit a message from node, and on the client side, your Javascript listen to those messages. 您只是从节点emit一条消息,而在客户端,您的Javascript会侦听这些消息。 They can contain any data needed to update the page, with the help of JQuery for example. 它们可以包含更新页面所需的任何数据,例如借助JQuery。

  • To watch the .csv file you can use fs.watch() as suggested below. 要观看.csv文件,可以按照以下建议使用fs.watch()

You could use fs.watch() ( http://nodejs.org/api/fs.html#fs_fs_watch_filename_options_listener ), but it does depend on the system node is running on. 您可以使用fs.watch()http://nodejs.org/api/fs.html#fs_fs_watch_filename_options_listener ),但这确实取决于运行的系统节点。 With that, you could use websockets to notify anyone 'watching' that file. 这样,您可以使用websockets通知任何“正在监视”该文件的人。

I believe that is pretty much what meteor does (haven't used it though, just did a little research on it). 我相信,这几乎就是流星所做的(虽然没有使用过,只是做了一些研究)。

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

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