Http Server (sampler)
This node.js Http server samples a metric and serves it at regular intervals.
var http = require('http');
var number = 0;
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var num = number.toString();
res.end('Hello World ' + num + '\n' );
}).listen(1337, '127.0.0.1');
var interval = setInterval(function(){
number++;
},2000);
console.log('Server running at http://127.0.0.1:1337/');
Http Client (poller)
This node.js Http client polls (requests) data at regular intervals.
To test start server on a terminal, then client on a different terminal.
To test start server on a terminal, then client on a different terminal.
var http = require('http');
var interval = setInterval(function(){
http.get("http://127.0.0.1:1337", function(res) {
console.log("Got response: " + res.statusCode);
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
},2000);
Http Client (agent)
This node.js Http client, when requested redirects GET request to the server on port 1337 and returns the response on port 1338.
To test start server on a terminal, then agent on a different terminal. On a browser request/refresh "http://127.0.0.1/1338".
To test start server on a terminal, then agent on a different terminal. On a browser request/refresh "http://127.0.0.1/1338".
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
http.get("http://127.0.0.1:1337", function(_res) {
console.log("Got response: " + _res.statusCode);
_res.on('data', function (chunk) {
res.end('BODY: ' + chunk);
console.log('BODY: ' + chunk);
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
}).listen(1338, '127.0.0.1');
No comments :
Post a Comment