Skip to content

Instantly share code, notes, and snippets.

@johnelliott
Last active October 11, 2022 18:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnelliott/5c53d769803aafaad11f1d0088638a97 to your computer and use it in GitHub Desktop.
Save johnelliott/5c53d769803aafaad11f1d0088638a97 to your computer and use it in GitHub Desktop.
Streaming HTML standard input demo
#! /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