私はnode.jsに本当に慣れていないので、明らかな間違いを犯している場合はご容赦ください。
node.js を理解するために、基本的に次のような Web サーバーを作成しようとしています。1) ルート URL (localhost:8000/) がヒットするたびに「hello world」を追加してページを更新します。2) ユーザーは別の URL (localhost:8000/getChatData) に移動でき、トリガーされた URL (localhost:8000/) から構築されたすべてのデータが表示されます。
私が経験している問題: 1) レンダリングされたページにそのデータを表示する際に問題があります。get_data() を毎秒呼び出し、追加された出力を格納するデータ変数で画面を更新するタイマーがあります。具体的には、response.simpleText(200, data); の下のこの行。正しく動作していません。
ファイル
// Load the node-router library by creationix
var server = require('C:\\Personal\\ChatPrototype\\node\\node-router').getServer();
var data = null;
// Configure our HTTP server to respond with Hello World the root request
server.get("/", function (request, response) {
if(data != null)
{
data = data + "hello world\n";
}
else
{
data = "hellow world\n";
}
response.writeHead(200, {'Content-Type': 'text/plain'});
console.log(data);
response.simpleText(200, data);
response.end();
});
// Configure our HTTP server to respond with Hello World the root request
server.get("/getChatData", function (request, response) {
setInterval( function() { get_data(response); }, 1000 );
});
function get_data(response)
{
if(data != null)
{
response.writeHead(200, {'Content-Type': 'text/plain'});
response.simpleText(200, data);
console.log("data:" + data);
response.end();
}
else
{
console.log("no data");
}
}
// Listen on port 8080 on localhost
server.listen(8000, "localhost");
これを行うためのより良い方法があれば、私に知らせてください。目標は基本的に、サーバーが URL を呼び出して変数を更新し、更新されたデータを毎秒動的にレポート/表示する別の html ページを持つ方法を用意することです。
ありがとう