Last active
August 29, 2015 14:03
-
-
Save jackboberg/76ca1075bfe89e5c0483 to your computer and use it in GitHub Desktop.
Node server to simulate streaming log files
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
| 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); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: