Defaults:
1s response
port 3000
Point browser to http://<hostname>:<port>[?query=<timeout>]
Or run curl http://<hostname>:<port>[?query=<timeout>]
// Load the http module to create an http server. | |
var http = require('http'); | |
var url = require('url'); | |
var config = {}; | |
config.port = process.env.PORT || 3000; // default port 3000 | |
config.timeout = 1000; // default timeout (ms) | |
// Configure our HTTP server to respond with Hello World to all requests. | |
var server = http.createServer(function (request, response) { | |
response.writeHead(200, {"Content-Type": "text/plain"}); | |
console.log(request.url); | |
// Start time | |
response.write('\n start = ' + new Date().toString() + '\n'); | |
getmetadata('latest/meta-data/public-hostname/',function(d) {response.write('\n'+d+'\n')}); | |
getmetadata('latest/meta-data/instance-id/',function(d) {response.write('\n'+d+'\n')}); | |
var urlparts = url.parse(request.url,true); | |
var timeout = urlparts.query.query || config.timeout; | |
setTimeout(function() { | |
// End time | |
response.end('\n end = ' + new Date().toString() + '\n'); | |
}, timeout ); | |
}); | |
// Server listens on port from config | |
server.listen(config.port); | |
console.log("Server running at port "+config.port); | |
// GET metadata | |
var getmetadata = function(path,cb) { | |
var mdsvr = {}; | |
mdsvr.hostname= '169.254.169.254'; | |
mdsvr.path= path; | |
console.log("getting " + path); | |
http.get(mdsvr, function(res) { | |
res.setEncoding('utf8'); | |
var body = ''; | |
res.on('data', function(d) { body += d; }); | |
res.on('end', function() { cb(body); }); | |
}).on('error', function(e) { | |
console.log("Got error: " + e.message); | |
}); | |
} |