Last active
October 11, 2022 18:04
-
-
Save johnelliott/5c53d769803aafaad11f1d0088638a97 to your computer and use it in GitHub Desktop.
Streaming HTML standard input demo
This file contains 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
#! /usr/bin/env node | |
const http = require('http') | |
process.stdin.setRawMode(true) | |
process.stdin.resume() | |
process.stdin.setEncoding('utf8') | |
const server = http.createServer((req, res) => { | |
res.statusCode = 200 | |
res.setHeader('Content-Type', 'text/html') | |
res.write('<html>') | |
process.stdin.on('data', key => { | |
if (key === '\u0003') { | |
console.log('Control-c') | |
res.write('</html>') | |
res.end() | |
process.exit() | |
} | |
console.log('key', key) | |
res.write(key) | |
}) | |
}) | |
const hostname = 'localhost' | |
const port = 3000 | |
server.listen(port, hostname, () => { | |
console.log(`Server running at http://${hostname}:${port}/`) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment