node.js server, displaying HTML file which has jqGrid plugin included.

I have a simple HTML file which includes jqGrid plugin. I am using jqGrid plugin to have a tree grid in my HTML page.

Now, I am trying to host this HTML file in node.js server. My server.js looks like this

var http = require('http');
var fs = require('fs');
var path = require('path');

http.createServer(function (request, response) {

console.log('request starting...');

var filePath = '.' + request.url;
console.log('filePath : '+filePath);

if (filePath == './')
    filePath = './tree.html';

var extname = path.extname(filePath);
console.log('extname : '+extname);

var contentType = 'text/html';

switch (extname) {
    case '.js':
        contentType = 'text/javascript';
        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);

So far, I am able to display my HTML content in browser [http://localhost:8125/]

Part of my HTML(tree.html) file looks like this

jQuery("#treegrid").jqGrid({ 
          url: 'tree.json', 
          datatype: 'json', 
          //mtype: 'GET', 
          colNames: [/* "ID",  */"Col 1", "Col 2",.. ],
          colModel: [/* { 
              name: 'id', 
              index: 'id', 
              width: 1, 
              hidden: true, 
              key: true 
          },  */{ ...

If you can notice, I have specified 'tree.json' as URL attribute to load the tree grid. That is just reading a static file to load the tree grid with sample data.

Problem : Now, when I try to access my HTML file using [http://localhost:8125/] I get an 404 Not Found error for [http://localhost:8125/tree.json]

Quick Solution : I can specify the relative path of the file 'tree.json' and it works.

Both my HTML file tree.html and tree.json are in the same directory (/tree) and I start my node.js server from command prompt (terminal) like this

tree> node server.js 

I would like to know where I can place my tree.json in order to make my HTML work as intended.

Please feel free to ask any clarification.

Thanks in advance

You use the line

var filePath = '.' + request.url;

in your code which seems a little strange. Probably in case of request of 'tree.json' you will have .tree.json as the value of filePath instead of ./tree.json which you probably want to have. I think you should fix the code here.

Moreover it would be good to set 'application/json' as the value of contentType variable in case of .json extension in the same way like you set 'text/javascript' and 'text/css' for '.js' and '.css' files.