Created
February 13, 2013 15:39
-
-
Save agmcleod/4945478 to your computer and use it in GitHub Desktop.
node static server for repurpose. From: http://www.hongkiat.com/blog/node-js-server-side-javascript/
This file contains hidden or 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 sys = require("sys"), | |
my_http = require("http"), | |
path = require("path"), | |
url = require("url"), | |
filesys = require("fs"); | |
my_http.createServer(function(request,response){ | |
var my_path = url.parse(request.url).pathname; | |
var full_path = path.join(process.cwd(),my_path); | |
path.exists(full_path,function(exists){ | |
if(!exists){ | |
response.writeHeader(404, {"Content-Type": "text/plain"}); | |
response.write("404 Not Found\n"); | |
response.end(); | |
} | |
else{ | |
filesys.readFile(full_path, "binary", function(err, file) { | |
if(err) { | |
response.writeHeader(500, {"Content-Type": "text/plain"}); | |
response.write(err + "\n"); | |
response.end(); | |
} | |
else{ | |
response.writeHeader(200); | |
response.write(file, "binary"); | |
response.end(); | |
} | |
}); | |
} | |
}); | |
}).listen(8080); | |
sys.puts("Server Running on 8080"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment