Skip to content

Commit

Permalink
add a server that serves with enough CORS headers to get modules to l…
Browse files Browse the repository at this point in the history
…oad properly
  • Loading branch information
mikesamuel committed May 21, 2018
1 parent 9b1d992 commit 8618179
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
# Emacs droppings
*~
/node_modules
6 changes: 4 additions & 2 deletions examples/alice-bob-carol/index.html
@@ -1,12 +1,14 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Module Keys Example Alice, Bob, and Carol</title>
<script type=module src="./index.js"></script>
</head>
<body>
This doesn't work via file: since <tt>import</tt> uses CORS.
To load in chrome, run <nobr><tt>python -m SimpleHTTPServer</tt></nobr> in this directory.
That serves from :8000 by default.
To load in chrome, run <nobr><tt>node server</tt></nobr> from the project root directory.
You might have to <nobt><tt>npm install --no-save serve-static</tt></nobr> first.
<p>
Logs to console.
</body>
Expand Down
32 changes: 32 additions & 0 deletions server.js
@@ -0,0 +1,32 @@
#!/usr/bin/env node

const http = require('http')
const serveStatic = require('serve-static')

const port = 8000
const origin = `http://localhost:${port}`

const staticServer = serveStatic(
'examples',
{
setHeaders(res, path, stat) {
res.setHeader('X-Content-Type-Options', 'nosniff')
res.setHeader('Access-Control-Allow-Origin', origin)
}
})

const server = http.createServer(
(req, res) => {
staticServer(
req, res,
() => {
res.writeHead(
404,
{ Location: `${origin}/alice-bob-carol/index.html` })
res.end()
})
})

server.listen(8000)

console.log(`Try ${origin}/alice-bob-carol/index.html`)

0 comments on commit 8618179

Please sign in to comment.