Simple node.js example: output ps command

Language: JavaScript

// This node.js example wraps the result of running "ps -a" in HTML
// and serves it at localhost:8080. It uses a meta tag
// to refresh the display every 5 seconds.

var sys = require('sys'), 
http = require('http'),
command = "ps -a";

function buildHtml(str) {
  var head = '<head><meta http-equiv="refresh" content="5"></head>';
  var body = '<body><pre>' + str + '</pre></body>';
  return ['<html>', head, body, '</html>'].join('\n');
}

http.createServer(function(req, res) {
  sys.exec(command).addCallback(function (stdout, stderr) {
    res.sendHeader(200, {'Content-Type': 'text/html'});
    res.sendBody(buildHtml(stdout));
    res.finish();
  });
}).listen(8080);

sys.puts('Server running at http://127.0.0.1:8080/');
Reveal More
Added 9 months ago by Devo_normal gbuesing