Skip to content

Commit

Permalink
creating of server in node.js to serve the project files
Browse files Browse the repository at this point in the history
  • Loading branch information
EvandroLG committed Dec 31, 2012
1 parent 46a361b commit 8a62ec4
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions server.js
@@ -0,0 +1,28 @@
var http = require('http');
var fs = require('fs');
var url = require('url');
var mimeTypes = {
'html': 'text/html',
'css': 'text/css',
'js': 'text/js'
};

var onHead = function(response, content, statusCode, extension){
var mimeType = mimeTypes[extension];

response.writeHead(statusCode, { 'Content-Type': mimeType });
response.end(content, 'utf-8');
};

http.createServer(function(request, response){
var pathname = '.' + url.parse(request.url, true).pathname;

fs.readFile(pathname, function(error, content){
if(error){
onHead(response, content, 400, 'html');
}

var extension = pathname.split('.')[2];
onHead(response, content, 200, extension);
});
}).listen(9000);

0 comments on commit 8a62ec4

Please sign in to comment.