Commit 474f5397 authored by Ricco Førgaard's avatar Ricco Førgaard
Browse files

A little more error handling in the server.

parent 5c7a9758
......@@ -10,11 +10,6 @@ var root = './www',
return !isValidFile;
},
write404 = function (response) {
response.writeHead(404, {'Content-Type': 'text/plain'});
response.end(':-(');
},
writeFileResponse = function (path, file, response) {
var contentType = (path.indexOf('.html') !== -1)
? 'text/html'
......@@ -25,14 +20,13 @@ var root = './www',
},
serveFile = function (path, response) {
console.log("Serving index.html");
var realPath = (path === '/')
? '/index.html'
: path;
fs.readFile(root + realPath, function (err, file) {
if (err || isNotAfile(file)) {
write404(response);
die(response, 404, 'File not found');
} else {
writeFileResponse(realPath, file, response);
}
......@@ -40,7 +34,6 @@ var root = './www',
},
serveData = function (response) {
console.log("Serve data");
var result = data.map(function (item, index) {
item.key = index;
return item;
......@@ -63,6 +56,19 @@ var root = './www',
});
},
die = function (response, code, msg) {
var status = (code === undefined)
? 500
: code,
message = (msg === undefined)
? 'Internal server error'
: msg;
console.log('Error: ' + code + ', ' + msg);
response.writeHead(code, {'Content-Type': 'text/plain'});
response.end(message);
}
server = http.createServer(function (request, response) {
console.log(request.method, request.url);
if (request.method === 'GET') {
......@@ -73,5 +79,7 @@ var root = './www',
}
} else if (request.method === 'POST') {
handlePOST(request, response);
} else {
die(response, 500, 'HTTP verb ' + request.method + ' not implemented.');
}
}).listen(8000);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment