Skip to content

Instantly share code, notes, and snippets.

@Nek
Created July 26, 2012 20:20
Show Gist options
  • Select an option

  • Save Nek/3184271 to your computer and use it in GitHub Desktop.

Select an option

Save Nek/3184271 to your computer and use it in GitHub Desktop.
Simple server on node.js
var http = require("http");
var fs = require("fs");
var path = require("path");
var url = require("url");
var mimeTypes = {
"html": "text/html",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"png": "image/png",
"js": "text/javascript",
"css": "text/css"
};
var server = http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname;
uri = "./" + uri;
if (uri.charAt(uri.length - 1) == "/") {
uri += "index.html";
}
if (uri.indexOf("..") != -1) {
response.writeHead(403);
response.end();
}
var filename = path.join(process.cwd(), uri);
console.log("\tAttempting to serve: " + filename);
path.exists(filename, function(exists) {
if (!exists) {
console.log("File not found: " + filename);
response.writeHead(404);
response.end("Sorry, the file you requested was not found. Don't let it ruin your day! :)");
return;
}
var mimeType = mimeTypes[path.extname(filename).split(".")[1]];
response.writeHead(200, {'Content-Type':mimeType});
var fileStream = fs.createReadStream(filename);
fileStream.pipe(response);
});
}).listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment