http
でサーバーを書いていますnode.js
。Server
オブジェクトには、要求に応じてクライアントに送信する必要があるいくつかのフィールドがあります。status()
これが、に渡す必要がある理由router.route()
です。つまり、(リクエストが解析された後に) 内部から呼び出して、更新変数値を返すことができます。問題は、status()
が呼び出されたときに、フィールド値ではなくオブジェクト リテラルが出力されることです。
コンストラクタServer
は次のとおりです。
this.server = net.createServer(connectionHandler);
this.resourceMap = resourceMap;
this.rootFolder = rootFolder;
this.isStarted = false;
this.startedDate = undefined;
this.port = undefined;
this.numOfCurrentRequests = 0;
function status() {
return {
"isStarted" : this.isStarted,
"startedDate" : this.startedDate,
"port" : this.port,
"resourceMap" : this.resourceMap,
};
}
function connectionHandler(socket) {
console.log('server connected');
console.log('CONNECTED: ' + socket.remoteAddress +':'+ socket.remotePort);
socket.setEncoding('utf8');
socket.on('data',function(data) {
this.numOfCurrentRequests += 1;
router.route(status,data,socket,handle,resourceMap,rootFolder);
});
}
this.startServer = function(port) {
this.port = port;
this.isStarted = true;
this.startedDate = new Date().toString();
this.server.listen(port, function() {
console.log('Server bound');
});
}
}
そして、ステータスが内部から呼び出されるrouter.route()
と、
function status() {
return {
"isStarted" : this.isStarted,
"startedDate" : this.startedDate,
"port" : this.port,
"resourceMap" : this.resourceMap,
};
}
私が理解しているように、関数は変数であるため、値で渡されます。私の問題を解決する方法はありますか?
ありがとう