如何用节点js实现这个逻辑

时间:2015-11-12 18:36:44

标签: node.js

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

我想这样做:

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.

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

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

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)

我非常感谢有任何想法,谢谢

1 个答案:

答案 0 :(得分:-1)

我会使用流监听这些文件的事件,它对性能更好,也可以监听事件(如果它有数据),如果它们有任何数据,那么我会读取它并将其传输给其他可写流(如您的http响应),我更喜欢使用流和事件而不是使用计时器,例如:

@ECHO OFF

FOR /F "tokens=*" %%a in ('find /I "Drink:" ^<source.txt') do SET OUTPUT=%%a
echo %output%

我希望这可以帮到你!