Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created November 29, 2017 23:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save isaacs/cc77f3091cde7dae2fbed74e1440044d to your computer and use it in GitHub Desktop.
Save isaacs/cc77f3091cde7dae2fbed74e1440044d to your computer and use it in GitHub Desktop.
const s = require('http').createServer((q, s) => {
console.error(q.method, q.url)
switch (q.url) {
case '/':
return html(s)
case '/app.js':
return js(s)
default:
s.statusCode = 404
s.setHeader('content-type', 'text/plain')
s.end('Not found: ' + q.url + '\n')
}
})
const html = s => {
s.setHeader('content-type', 'text/html')
s.end(`<!doctype html>
<html>
<script type=module>
import app from './app.js'
app()
</script>
`)
}
const js = s => {
s.setHeader('content-type', 'application/javascript')
s.end(`
const el = document.createElement('h1')
el.innerHTML = 'blerg'
document.body.appendChild(el)
const app = () => console.log('hi from app.js')
export default app
`)
}
s.listen(8080)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment