I am new to node js. I am following the link. But this always executing the response.writeHead(404); So I can't able to view the index.html
http://thecodinghumanist.com/blog/archives/2011/5/6/serving-static-files-from-node-js
This is my web server node js code.
var http = require('http');
var path = require('path');
var fs = require('fs');
http.createServer(function (request, response) {
console.log('request starting...');
var filePath = "."+request.url;
if (filePath == './')
filePath = '/index.html';
else
console.log(request.url);
var extname = path.extname(filePath);
var contentType = 'text/html';
switch (extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
}
fs.exists( filePath, function (exists) {
console.log(filePath);
if (exists) {
fs.readFile(filePath, function (error, content) {
if (error) {
console.log(error);
response.writeHead(500);
response.end();
}
else {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end(content, 'utf-8');
}
});
}
else {
response.writeHead(404);
response.end();
}
});
}).listen(8125);
My questions.
- This is not showing the index.html.
- How can i send response or html file based different requests ?
eg:
http://localhost:8125-> index.htmlhttp://localhost:8125/firstpage.html-> firstpage.html - Do we need to put the html files in the same directory which contains server.js (web server) ?
I also tried the same in cloud9 ide.

I found a similar issue in SO. but I did n't get a clear idea how to fix it. Node.js - Socket.io client file not being served by basic node web server Thanks in advance.