Skip to content

Instantly share code, notes, and snippets.

@jackboberg
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save jackboberg/76ca1075bfe89e5c0483 to your computer and use it in GitHub Desktop.

Select an option

Save jackboberg/76ca1075bfe89e5c0483 to your computer and use it in GitHub Desktop.
Node server to simulate streaming log files
var http = require('http');
var stream = require('stream');
var util = require('util');
var PassThrough = stream.PassThrough ||
require('readable-stream').PassThrough;
var server = http.createServer(function (req, res){
var timestamp = Date.now();
var stream = new PassThrough();
stream.pipe(res);
stream.pipe(process.stdout);
stream.write(util.format('%s: connected\n', timestamp));
var interval = setInterval(function (){
stream.write(util.format('%s: new line @%s\n', timestamp, Date.now()));
}, process.argv[3] || 500);
req.on('close', function (){
stream.end(util.format('%s: disconnected\n', timestamp));
clearInterval(interval);
});
});
server.listen(process.argv[2] || 8080);
@jackboberg
Copy link
Copy Markdown
Author

Usage:

node logServer.js [port] [interval]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment