繁体   English   中英

如何用 node js 实现这个逻辑

[英]how to implement this logic with node js

我是 node.js 的新手,我想用 node.js 替换我在 C# 中创建的应用程序,但是 node 的异步性质让我失望:

我想这样做:

1- Read N files, parse them and fill an array with the values from the files. 
2- Then every 30 seconds I need to read the files again and get the latest values in the file, added them to the array.
3- but immediately after step 1 is complete I can start a web server that will receive request from a browser. the server needs to send to the client some values form the array.

所以我需要网络服务器的连续循环(在加载初始值之后)能够回复客户端,同时我需要定期刷新数组

使用现有的应用程序,我这样做:

1- read files , fill array
2- start a thread to server the clients (until app is closed)
3- start a thread to read the files again every N seconds and add new values to array  (until app is closed)

我将不胜感激任何有关此问题的想法,谢谢

我会使用流来侦听这些文件的事件,这对性能更好,并且还可以侦听事件(比如它是否有数据),如果它们有任何数据,那么我会读取它并将其通过管道传输到其他可写流(比如你的 http 响应) ) ,我更喜欢使用流和事件而不是使用计时器,例如:

var fs = require('fs');
var stream = fs.createReadStream(path);

stream.on('data',function(d){
  //just pipe it to your writable stream (you can use the http response)
});

我希望这可以帮助你!

暂无
暂无

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

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