Skip to content

Instantly share code, notes, and snippets.

@mxschmitt
Created August 24, 2020 09:03
Show Gist options
  • Save mxschmitt/d4f2c04a4e1267158eaceb84a8f06ae1 to your computer and use it in GitHub Desktop.
Save mxschmitt/d4f2c04a4e1267158eaceb84a8f06ae1 to your computer and use it in GitHub Desktop.
Node.js web server with top-level-await and async for loop
// Run it with: node --harmony-top-level-await test.mjs
import { createServer } from 'http'
import { on } from 'events'
const PORT = 3000
const reqs = on(createServer().listen(PORT), "request")
console.log(`Listening on port ${PORT}`)
for await (const [req, res] of reqs)
res.end(`User-Agent: ${req.headers["user-agent"]}`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment