Skip to content

Instantly share code, notes, and snippets.

@ehgoodenough
Created May 4, 2013 20:10
Show Gist options
  • Save ehgoodenough/5518583 to your computer and use it in GitHub Desktop.
Save ehgoodenough/5518583 to your computer and use it in GitHub Desktop.
Routing and serving webpages through NodeJS. The system is currently configured to accept and return any request for any document.
var app = require("http").createServer(handler).listen(1337);
//var io = require("socket.io").listen(app, {log: false});
var path = require("path");
var url = require("url");
var fs = require("fs");
function handler(request, response)
{
var action = "." + url.parse(request.url, true).pathname;
if(action == "./") {action = "./index.html";}
fs.readFile(action, function(error, data)
{
if(error)
{
response.writeHead(500);
return response.end("Uh oh!!");
}
var extension = path.extname(action).substring(1);
response.writeHead(200, {"content-type": "text/" + extension});
response.end(data);
});
}
console.log("Server is running at 127.0.0.1:1337");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment