Skip to content

Instantly share code, notes, and snippets.

@whtsky
Created April 25, 2023 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whtsky/a5db3b9609be5a5bc5e83a4bb0638eeb to your computer and use it in GitHub Desktop.
Save whtsky/a5db3b9609be5a5bc5e83a4bb0638eeb to your computer and use it in GitHub Desktop.
const http2 = require("http2");
const fs = require("fs");
const port = 8433;
const server = http2.createSecureServer({
key: fs.readFileSync("localhost.key"),
cert: fs.readFileSync("localhost.crt"),
});
server.on("error", (err) => console.error(err));
server.on("stream", (stream, headers) => {
stream.respond({
"content-type": "text/html; charset=utf-8",
":status": 200,
});
stream.write("<p>Hello World!</p>");
let i = 0;
const interval = setInterval(function () {
console.log("Write: ", i);
stream.write(i + "\n");
i++;
}, 1000);
stream.on("close", () => {
clearInterval(interval);
});
});
server.listen(port, () => {
console.log(`HTTP/2 server running on port ${port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment