简体   繁体   中英

How can I automatically update a view using Node.js?

I have a cache and a cron-job for pulling/receiving and saving data. I want that the view updates itself. For testing it should update every 5 seconds. In this way I should see, when the data is pulled, saved to cache and finally will appear on the view.

I found out that socket.io can help me. I haven't found a suited example to my purpose. Can somebody help me?

Here is a snippet of my code from app.js:

 var dashboardData = "";

 //----fired when cache has changed
 myCache.on( "set", function( key, value ){
     stats = JSON.stringify(myCache.getStats());
     stats = JSON.parse(stats);
     console.log(stats.keys);
     var content = JSON.stringify(value);
     content = JSON.parse(content);
     dashboardData = content;
 });

 //----load dashboard view
 app.post('/dashboard', function(req, res) {
     projectName = req.body.selectProjectName;
     if(dashboardData == ""){
         var content = "";
         dashboard.dashboard(req,res, projectName, content); 
      } else {
         dashboard.dashboard(req,res, projectName, dashboardData.variable); 
      }
   });

我当前的解决方案:我添加了一个带有异步ajax调用的jquery javascript,每5秒更新一次视图。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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