Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created January 19, 2012 00:52
Show Gist options
  • Save isaacs/1636859 to your computer and use it in GitHub Desktop.
Save isaacs/1636859 to your computer and use it in GitHub Desktop.
<!doctype html>
<title>client-side script.js node example</title>
<script src="script.js"></script>
<p>This is a little html page</p>
alert("Hello, this is client-side script")
var fs = require("fs")
var script = fs.readFileSync("script.js")
var index = fs.readFileSync("index.html")
var port = process.env.PORT || 1337
require("http").createServer(function (req, res) {
switch (req.url) {
case "/index.html":
return serveFile(index, "text/html", res)
case "/script.js":
return serveFile(script, "text/javascript", res)
default:
res.writeHead(404, {})
res.end("not found")
}
}).listen(port)
function serveFile (contents, type, res) {
res.writeHead(200, { "content-type": type
, "content-length": contents.length })
res.end(contents)
}
console.log("Open in your web browser: http://localhost:"+port+"/index.html")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment