Created
March 13, 2012 02:18
-
-
Save Lull3rSkat3r/2026184 to your computer and use it in GitHub Desktop.
Super Simple Nodejs HTTP Server
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 http = require('http'); | |
var fs = require('fs'); | |
server = {} | |
/** | |
* Builds a url from the array object <b>path</b>. If anything needs to be appended to the path please add it in <b>file</b> | |
* @param file Any directories that need to be prepended to the string of the resource | |
* @param path An array of items that will be used to construct the url | |
* @author <a href="mailto:coreystubbs@microbialdev.com">Corey Stubbs</a> | |
* @since 0.1 | |
*/ | |
buildFileUrl = function(file, path){ | |
for(var i = 1; i < path.length; i++){ | |
file += path[i]; | |
if(i != path.length - 1) | |
file += "\\"; | |
} | |
// If they only provided the directory serve index.html | |
if((path.length == 3 && path[2] == "") || (path.length == 2)){ | |
file += "\\index.html"; | |
} | |
console.log("File is " + file); | |
return file; | |
}, | |
/** | |
* Serves a resouce found within the docs directory | |
* @param res The response object that will write to the client | |
* @param path The system path to the resource within the docs directory | |
* @author <a href="mailto:coreystubbs@microbialdev.com">Corey Stubbs</a> | |
* @since 0.1 | |
*/ | |
serveDocs = function(res,path){ | |
// TODO Check to see if the file returned is the correct one | |
try{ | |
var file = buildFileUrl(process.cwd()+"\\", path); | |
var contents = fs.readFileSync(file,'utf-8'); | |
res.writeHead(200, {'Content-Type': 'text/html'}); | |
res.end(contents); | |
} catch(e){ | |
console.log(e); | |
serve404(res,path); | |
} | |
}, | |
/** | |
* Serves a 404 resource not found error | |
* @param res The response object that will write to the client. | |
* @param path The path to the resource that is not found | |
* @author <a href="mailto:coreystubbs@microbialdev.com">Corey Stubbs</a> | |
* @since 0.1 | |
*/ | |
serve404 = function(res, path){ | |
res.writeHead(404, {'Content-Type': 'text/html'}); | |
res.end("Resource not found."); | |
console.log("Could not find path" + path); | |
}, | |
/** | |
* Should be called within the catch block of a try-catch block. | |
* @param res The response object that will write to the client | |
* @param e The exception caught in the try-catch block | |
* @author <a href="mailto:coreystubbs@microbialdev.com">Corey Stubbs</a> | |
* @since 0.1 | |
*/ | |
serve500 = function(res,e){ | |
res.writeHead(500, {'Content-Type': 'text/html'}); | |
res.write("Internal Server Error."); | |
res.end(e.toString()); | |
}, | |
startServer = function(){ | |
console.log(process.cwd()); | |
http.createServer(function(req,res){ | |
var url = require('url'); | |
var fullUrl = url.parse(req.url, true); | |
var path = fullUrl.pathname.split("/"); | |
serveDocs(res,path); | |
}).listen(8080, "127.0.0.1"); | |
} | |
startServer(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment