title: Using node.js to update HTML via external script?
body: <p>I'm trying to set up a Node.js application that will serve up an HTML page in the front-end while pinging an IP. The end result will hopefully be a static web page that will display the status of the IP being pinged.</p>
  
  <p>I have the HTML page running just fine using Express. It's basically just a static file with a single <code>&lt;a&gt;</code> whose text and color is changed by an external script. Temporarily, I have a button to toggle the displayed status, and so far it's looking good.</p>
  
  <p>Question is, how do I update the HTML based on the result gotten from the <code>Pinger</code> (explained below)?</p>
  
  <p>I have two Node.js source files. Obviously, the main one is <code>server.js</code>:</p>
  
  <pre><code>var express = require("express"),
  app     = express(),
  Pinger  = require("./pinger");
  
  app.use(express.static(__dirname));
  console.log("Using static files at /");
  app.use("/styles", express.static(__dirname + '/styles'));
  console.log("Using stylesheets at /styles");
  app.use("/res", express.static(__dirname + '/res'));
  console.log("Using resources at /res");
  
  app.listen(3000);
  console.log("Listening on port 3000");
  
  var pinger = new Pinger('google.com'); /* how the instance would be created */
  </code></pre>
  
  <p>And there's the <code>Pinger</code> module:</p>
  
  <pre><code>var ping  = require('ping');
  
  var Pinger = function() {
  
      this.host = arguments.length &gt; 0 ? arguments[0] : 'google.com'; 
      this.isOnline = false;
  
      this.pingAll = function pingAll() {
  
          console.log("&gt;&gt;PING: " + host);
          ping.sys.probe(host, function(isAlive) {
              if (isAlive) console.log("&gt;&gt;PONG: " + host);
              else console.log("Unable to reach " + host);
          });
  
          // somehow return isAlive
  
      };
  
  };
  
  module.exports = Pinger;
  </code></pre>
  
  <p>Obviously, right now, the pinger's <code>isOnline</code> can only be false because it's never updated. </p>
  
  <p>On a side note, how would I be able to update isOnline from inside the <code>ping.sys.probe</code> callback? I'm aware of the different scopes, and I'm pretty sure that just using <code>this.isOnline = isAlive</code> would be useless because <code>this</code> would refer to the callback (unless I'm wrong in the first place and over-thinking it).</p>