jqGrid プラグインを含む単純な HTML ファイルがあります。jqGrid プラグインを使用して、HTML ページにツリー グリッドを表示しています。
現在、この HTML ファイルを node.js サーバーでホストしようとしています。私のserver.jsは次のようになります
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);
これまでのところ、HTML コンテンツをブラウザー [http://localhost:8125/] に表示できます。
私のHTML(tree.html)ファイルの一部は次のようになります
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
}, */{ ...
お気付きかもしれませんが、ツリー グリッドをロードする URL 属性として「tree.json」を指定しました。これは、静的ファイルを読み取ってツリー グリッドにサンプル データをロードするだけです。
問題 : [http://localhost:8125/] を使用して HTML ファイルにアクセスしようとすると、[http://localhost:8125/tree.json] に対して404 Not Foundエラーが発生します。
簡単な解決策:ファイル「tree.json」の相対パスを指定でき、機能します。
HTML ファイルtree.htmlとtree.jsonの両方が同じディレクトリ (/tree) にあり、このようにコマンド プロンプト (ターミナル) から node.js サーバーを起動します。
tree> node server.js
HTML を意図したとおりに動作させるために、 tree.jsonを配置できる場所を知りたいです。
ご不明な点がございましたら、お気軽にお問い合わせください。
前もって感謝します