は、私は次のコードを持っています。キャプチャ返された機能は、JavaScriptで
このメソッドを外部から呼び出す方法を理解できません。
次のように私はそれを呼び出しています:
staticFile("D:\\Node Applications\\public\\home.html")
を私は変数innerFunc
内でそれをキャプチャすることができますし、応答は私が参照を持っているそのHTTPのserverResponseここで私はinnerFunc(response)
ように、内側の関数を呼び出すことができます私と一緒ですが、私はどのようにdata
paramを渡すことができるのか分かりません。
私は舞台裏で何が起こっているのか理解していません。誰でも説明できますか?私たちはJavaScriptのようなコードを頻繁に遭遇しますか?
EDIT: 物事を明確にする: 次のように別の方法があります:
次のように私は自分のノードサーバロジックから呼び出すfunction(data, response) {
response.writeHead(200, {
'Content-Type': 'application/json'
});
response.end(JSON.stringify(data));
}
:
http.createServer(function(req, res) {
// A parsed url to work with in case there are parameters
var _url;
// In case the client uses lower case for methods.
req.method = req.method.toUpperCase();
console.log(req.method + ' ' + req.url);
if (req.method !== 'GET') {
res.writeHead(501, {
'Content-Type': 'text/plain'
});
return res.end(req.method + ' is not implemented by this server.');
}
if (_url is something like //localhost:1337/employees) {
//call employee service which returns *data*.
// send the data with a 200 status code
return responder.sendJson(data, res);
});
} else {
// try to send the static file
/*res.writeHead(200);
res.end('static file maybe');*/
console.log('Inside else');
var staticInner = responder.staticFile("D:\\Node Applications\\public\\home.html");
staticInner(res);
}
// res.end('The current time is ' + Date.now())
}).listen(1337, '127.0.0.1');
1ことができますようにデータに渡す変数がありませんので、innerFuncに渡す変数が混乱しています。
あなたはその機能が何をすべきか理解していますか?あなたが 'data'を_pass_する方法を知らないなら(あなたが' data'を知らないと思うのですが、 'innerFunc(data、response)'として渡すので)、あなたはその機能が何をしているのか分かりません。それを知ることは良いことです。また、結果としてこれを得ることを期待しています: 'return readStream.pipe(response)'、または何か? – Parziphal
あなたは正しいです。私は私の質問を編集して、私は関数にそれを渡すために 'データ'変数を持っていないことがわかります。最初は、私はいくつかの理解が不足しているかもしれないと思ったが、データ変数が関数に渡す必要があると思われる。 関数から関数を返す理由を説明できますか?すべてのインスタンスは何のために必要なのでしょうか? ありがとう! – Adithya