0

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

Ayaz Pasha
  • 1,045
  • 6
  • 13
  • 28
  • You included `console.log('filePath : '+filePath);` in your code. Do you get some information about the request of `tree.json` in the console? – Oleg May 30 '12 at 10:10
  • FYI, the content of my **tree.json** looks like this { "page": 1, "total": 1, "records": 2, "rows": [] } and the console.log says [http://localhost:8125/tree.json?_search=false&nd=1338374206180&rows=20&page=1&sidx=&sord=asc] 404 Not Found. – Ayaz Pasha May 30 '12 at 10:39
  • I think that the error "404 Not Found" means that you have problem with the path of the file and not with the content of the file. – Oleg May 30 '12 at 10:40
  • @Oleg As you can see the console.log statement has the URL and some extra query parameters, are these parameters added by the jqGrid plugin? If so, then how can I get rid of it? I can post my jqGrid settings (attributes). Instead of looking for **tree.json**, its looking for **tree.json?blah..**. The conole.log says request starting... filePath : ./tree.json?_search=false&nd=1338374206180&rows=20&page=1&sidx=&sord=asc extname : .json?_search=false&nd=1338374206180&rows=20&page=1&sidx=&sord=asc – Ayaz Pasha May 30 '12 at 10:44
  • 1
    Please read my answer on your question below. I wrote it a little later as I wrote the above comment. You use `var filePath = '.' + request.url;` So the `filePath` should start with `'.'` and it's the main problem which I see to fix the error "404 Not Found". The jqGrid code and the content of `tree.json` should be irrelevant for the "Not Found" error. – Oleg May 30 '12 at 10:46

1 Answers1

0

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.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Though the URL attribute in my HTML is url:'tree.json', the request is '/tree.json' so this code in server.js is not making any problem. But, as you said, it is surely the path which is making problem. When I specify the absolute path of 'tree.json' like url:'/user/blah/blah/tree/tree.json', it works fine. I have also added the contentType for .json case is server.js but with no much help. – Ayaz Pasha May 30 '12 at 11:25
  • 1
    @AyazPasha: I don't know node.js server, but I find strange that the function `path.exists` can find the file `'./tree.html'`, but can't find the file `'./tree.json'`. As a workaround you can use `url` in form of `baseUrl` + `'/tree.json'` where `baseUrl` you get from `window.location.pathname`. See [here](http://stackoverflow.com/a/2820725/315935). – Oleg May 30 '12 at 11:48