Skip to content

Instantly share code, notes, and snippets.

@19h
Created February 28, 2014 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 19h/9277718 to your computer and use it in GitHub Desktop.
Save 19h/9277718 to your computer and use it in GitHub Desktop.
A server written with plain TCP sockets in Node. Needs files residing in ./static/.
var methods = [ "GET" ];
var protocol = [ "HTTP/1.1", "HTTP/1.0" ];
var errorCodes = {
100: "Continue",
101: "Switching Protocols",
102: "Processing",
200: "OK",
201: "Created",
202: "Accepted",
203: "Non-Authoritative Information",
204: "No Content",
205: "Reset Content",
206: "Partial Content",
207: "Multi-Status",
208: "Already Reported",
226: "IM Used",
300: "Multiple Choices",
301: "Moved Permanently",
302: "Found",
303: "See Other",
304: "Not Modified",
305: "Use Proxy",
307: "Temporary Redirect",
308: "Permanent Redirect",
400: "Bad Request",
401: "Unauthorized",
402: "Payment Required",
403: "Forbidden",
404: "Not Found",
405: "Method Not Allowed",
406: "Not Acceptable",
407: "Proxy Authentication Required",
408: "Request Timeout",
409: "Conflict",
410: "Gone",
411: "Length Required",
412: "Precondition Failed",
413: "Payload Too Large",
414: "URI Too Long",
415: "Unsupported Media Type",
416: "Requested Range Not Satisfiable",
417: "Expectation Failed",
422: "Unprocessable Entity",
423: "Locked",
424: "Failed Dependency",
425: "Unassigne",
426: "Upgrade Required",
427: "Unassigne",
428: "Precondition Required",
429: "Too Many Requests",
430: "Unassigne",
431: "Request Header Fields Too Large",
500: "Internal Server Error",
501: "Not Implemented",
502: "Bad Gateway",
503: "Service Unavailable",
504: "Gateway Timeout",
505: "HTTP Version Not Supported",
506: "Variant Also Negotiates",
507: "Insufficient Storage",
508: "Loop Detected",
509: "Unassigne",
510: "Not Extended",
511: "Network Authentication Required"
};
var makeResponse = function (err, headers) {
if (!err) err = 200;
var res = protocol[0] + " " + err + " " + errorCodes[err];
if (headers) {
res += "\n" + Object.keys(headers).map(function (header) {
return header + ": " + headers[header];
}).join("\n");
}
return res;
}
var crypto = require("crypto"),
fs = require("fs"),
net = require("net");
net.createServer(function (sock) {
sock.on("data", function (data) {
data = data.toString().split("\r\n");
var sign = data[0].split(" ");
var method = sign[0],
path = sign[1],
protocol = sign[2];
if ( !~methods.indexOf(method) )
return sock.end(makeResponse(501));
var headers = data.slice(1, data.indexOf(""));
var rpath = "./static" + path;
return fs.exists(rpath, function (exists) {
if (!exists) return sock.end(makeResponse(404));
fs.stat(rpath, function (err, stat) {
if (stat.isDirectory()) {
if (rpath[rpath.length - 1] !== "/") {
return sock.end(makeResponse(307, {
Location: path + "/"
}))
}
return fs.exists(rpath + "/index.html", function (exists) {
if (!exists) return sock.end(makeResponse(404));
sock.write(makeResponse(200) + "\n\n")
fs.createReadStream(rpath + "/index.html").pipe(sock);
});
}
sock.write(makeResponse(200) + "\n\n")
fs.createReadStream(rpath).pipe(sock);
})
})
})
sock.on("error", new Function)
}).listen(8045);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment