Skip to content

Instantly share code, notes, and snippets.

@arasatasaygin
Last active June 4, 2018 02:56
Show Gist options
  • Select an option

  • Save arasatasaygin/5f40122b7c8c7715d447 to your computer and use it in GitHub Desktop.

Select an option

Save arasatasaygin/5f40122b7c8c7715d447 to your computer and use it in GitHub Desktop.
Sample Node HTTP Server
var http = require('http'); //HTTP server api
var fs = require('fs'); //For working with local files
var server = new http.Server(); //Create a new HTTP server
server.listen(8000); //Run it in port 8000
//When the server gets a new request, run this function to handle it
server.on("request", function(request, response) {
var url = require('url').parse(request.url); // Parse the requested url
if(url.pathname === "test/delay") {
var delay = parseInt(url.query) || 2000;
response.writeHead(200, {"Content-Type" : "text/plain; charset=UTF-8"}); //Set the response status code and headers
response.write("Sleeping for " + delay + " miliseconds..."); //Response body
setTimeout(function() {
response.write("done");
response.end();
}, delay);
} else if(url.pathname === "/test/mirror") {
response.writeHead(200, {"Content-Type" : "text/plain; charset=UTF-8"}); //Set the response status code and headers
response.write(request.method + ": " + request.url + " HTTP/" + request.httpVersion + "\r\n"); //Response body
for(var h in request.headers) {
response.write(h + ": " + request.header[h] + "\r\n");
}
response.write("\r\n"); //End header with an extra blank line
request.on("data", function(chunk) {response.write(chunk);}); //When a chunk of the request body, add it to the responde
request.on("end", function(chunk) {responde.end();}); //When request ends, the response is done too
} else {
//Get local filename and guess its content type from extension
var filename = url.pathname.substring(1);
var type;
switch(filename.substring(filename.lastIndexOf(".") + 1)) {
case "html":
case "htm": type = "text/html; charset=UTF-8"; break;
case "js": type = "application/javascript; charset=UTF-8"; break;
case "css": type = "text/css; charset=UTF-8"; break;
case "txt": type = "text/plain; charset=UTF-8"; break;
case "manifest": type = "text/cache-manifest; charset=UTF-8"; break;
default: type = "application/octet-stream"; break;
}
//Read the file async and pass the content as a single chunk to the callback function.
//For really large files, using the streaming API with fs.createReadStream() would be better
fs.readFile(filename, function(err, content) {
if(err) { //If we couldn't read the file for some reason
response.writeHead(404, { //Send a 404 not found status
"Content-Type": "text/plain; charset=UTF-8"
});
response.write(err.message); //Response body as error message
response.end();
} else {
response.writeHead(200, {
{"Content-Type": type}
});
response.write(content); //Send file contents as a response body
response.end();
}
});
}
});
//From: JS the definitive guide
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment