nanha
4/16/2012 - 3:21 PM

example to use nodetime module within local environment

example to use nodetime module within local environment

// nodetime upload server example
var http = require('http');
var upload_port = 12345;
var server = http.createServer(function(req, res) {
  if (req.method === 'POST' && req.url === '/upload') {
    var data = '';
    req.on('data', function(chunk) {
      data += chunk;
    });
    req.on('end', function() {
      var sample = JSON.parse(data);
      for (var key in sample) {
        console.log(key, sample[key]);
      }
      data = '';
    });
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('data uploaded.');
  }
}).listen(upload_port, function() {
  console.log("listening on port " + upload_port);
});
// nodetime for local use
var uphttp = require('http');
var nodetime = require('nodetime');
var upload_port = 12345;
nodetime.on('sample', function(sample) {
  var upload = uphttp.request({ port: upload_port,
                                method: 'POST',
                                path: '/upload'
                              });
  upload.end(JSON.stringify(sample));
  upload.on('response', function(res) {
    var data = '';
    res.on('data', function(chunk) {
      data += chunk;
    });
    res.on('end', function() {
      console.log(data);
      data = '';
    });
  });
  upload.on('error', function(e) {
    console.log(e.toString());
  });
});
nodetime.profile({headless: true});

var port = 8080;
var http = require('http');
var server = http.createServer(function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('hello world\n');
}).listen(port, function() {
  console.log("listening on port " + port);
  console.log("upload to port " + upload_port);
});