Skip to content

Instantly share code, notes, and snippets.

@FND
Last active May 31, 2017 06:44
Show Gist options
  • Save FND/b0db6be61b71b19acf555171be0f9022 to your computer and use it in GitHub Desktop.
Save FND/b0db6be61b71b19acf555171be0f9022 to your computer and use it in GitHub Desktop.
Express streaming sample
"use strict";
let express = require("express");
const LIPSUM = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
let app = express();
app.get("/", (req, res) => {
res.status(200);
let i = 0;
let emit = _ => {
res.write(`[${++i}] ${LIPSUM}\n`);
setTimeout(emit, 100);
};
emit();
setTimeout(_ => {
res.write("~~~ EOS ~~~\n");
res.end();
}, 2000);
});
let server = app.listen(3000, () => {
let addr = server.address();
// eslint-disable-next-line no-console
console.log(`→ http://${addr.address}:${addr.port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment