Skip to content

Instantly share code, notes, and snippets.

@shigeki
Created April 11, 2012 04:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shigeki/2356957 to your computer and use it in GitHub Desktop.
Save shigeki/2356957 to your computer and use it in GitHub Desktop.
example to use nodetime module within local environment
// 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);
});
// 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);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment