簡體   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