Skip to content

Instantly share code, notes, and snippets.

@jtyjty99999
Created July 11, 2014 06:41
Show Gist options
  • Select an option

  • Save jtyjty99999/85efb1143f5dc21c61ec to your computer and use it in GitHub Desktop.

Select an option

Save jtyjty99999/85efb1143f5dc21c61ec to your computer and use it in GitHub Desktop.
ch1
var http = require('http'),
url = require('url'),
path = require('path'),
fs = require('fs'),
config = require('./config.js')
var PORT = config.PORT;
var server = http.createServer(function (request, response) {
response.setHeader("Server", "ICBUNBSERVER");
var pathname = url.parse(request.url).pathname;
if (pathname.slice(-1) === "/") {
pathname = pathname + config.Welcome.file;
}
var realPath = path.join("static", path.normalize(pathname.replace(/\.\./g, "")));
fs.exists(realPath, function (exists) {
if (!exists) {
response.writeHead(404, {
'Content-Type' : 'text/plain'
});
response.write("This request URL " + pathname + " was not found on this server.");
response.end();
} else {
fs.readFile(realPath, "binary", function (err, file) {
if (err) {
response.writeHead(500, {
'Content-Type' : 'text/plain'
});
response.end(err);
} else {
response.writeHead(200, {
'Content-Type' : 'text/html'
});
response.write(file, "binary");
response.end();
}
});
}
});
});
server.listen(PORT);
console.log("Server runing at port: " + PORT + ".");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment