最近Nodejitsuのブログを読みましたが、このコードがどのように機能するのか疑問に思っています。
var fs = require('fs'),
http = require('http'),
httpProxy = require('../lib/node-http-proxy');
module.exports = function (logging) {
// Code here is run when the middleware is initially loaded.
var logFile = fs.createWriteStream('./requests.log');
return function (request, response, next) {
// Code here is run on each request.
if (logging) {
logFile.write(JSON.stringify(request.headers, true, 2));
}
next();
}
}
このコードの説明は次のとおりです。
このミドルウェアは、非常に単純なロギング用です。各リクエストのヘッダーをログ ファイルに書き込みます。
エクスポートされた上記のモジュールは、次のように使用できます。
httpProxy.createServer(
require('./example-middleware')(true),
8000, 'localhost'
).listen(9000)
next()
すべてのリクエストで呼び出される上記のメソッドのコードはどのようになっていますか? 使い方はとても簡単です: 上記のモジュールを require すると、毎回呼び出されます。