Aby si sa dostal k ostatným súborom, tak musíš vypočítať cestu v súborovom systéme z request.url:
var http = require('http');
var fs = require('fs');
var path = require('path');
http.createServer(function(request, response) {
console.log(request.method + ' ' + request.url);
var filePath = '.' + request.url;
if (filePath == './')
filePath = './index.html';
if (filePath.indexOf('/../') !== -1) {
response.writeHead(400);
response.end();
} else {
var extname = path.extname(filePath);
var contentType = 'text/plain';
switch (extname) {
case '.js': contentType = 'text/javascript'; break;
case '.html': contentType = 'text/html'; break;
case '.css': contentType = 'text/css'; break;
}
path.exists(filePath, function(exists) {
if (exists) {
fs.readFile(filePath, function(error, content) {
if (error) {
response.writeHead(500);
response.end();
} else {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
}
});
} else {
response.writeHead(404);
response.end();
}
});
}
}).listen(8125);
console.log('Server running at http://127.0.0.1:8125/');
Prípony pre určenie správneho Content-Type si môžeš pridávať podľa potreby.