Created
January 19, 2012 00:52
-
-
Save isaacs/1636859 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<title>client-side script.js node example</title> | |
<script src="script.js"></script> | |
<p>This is a little html page</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alert("Hello, this is client-side script") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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