Created
April 11, 2012 04:42
-
-
Save shigeki/2356957 to your computer and use it in GitHub Desktop.
example to use nodetime module within local environment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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