2012-04-27 5 views
0

私はFlatiron Unionベースのアプリケーションで作業しています。ルートが実行される前にログを開発しているシンプルなロガーなので、何が起こったのかを正確には報告していないようです。私はUnionの例からロガーサンプルコードを取り出しました。ここで剥ぎ取らコードサンプルです:Flatiron Unionの "after"関数は "before"関数の前に実行されますか?

var 
    union = require('union') 
    , server; 

server = union.createServer({ 
    before: [ function (req,res) { 
    console.log('before'); 
    res.writeHead(404, {"Content-Type": "text/plain"}); 
    res.end("Hello World"); 
    } ], 
    after: [ 
    function LoggerStream() { 
     var stream = new union.ResponseStream(); 
     stream.once("pipe", function (req) { 
      console.log({res: this.res.statusCode, method: this.req.method}); 
     }); 
     return stream; 
    } 
    ] 
}); 

server.listen(8800); 
console.log('union running on 8800'); 

は、ここに私のコンソールに表示される内容です:報告されたステータスがなぜhttpサーバは、実際に404

を返さ200であることを

$ DEBUG=* node ./union.js 
union running on 8800 
{ res: 200, method: 'GET' } 
before 

注意これは順不同ですか?

答えて

1

ここindexzeroによって、私たちのメーリングリストからあなたの質問への最近の返信です:

スチュアート、

これは実際には正常な動作です。パイプ鎖から union.RoutingStreamインスタンスで構成されている: - > after0 -

union.ResponseStream()> after1 - > after2 - > ... - > aftern - > HTTP .Responseは()だから、

すぐ を起動された後、チェーン内のすべてのストリーム上pipeイベント( https://github.com/flatiron/union/blob/master/lib/routing-stream.js#L74-83参照)が、これらのストリームの誰もが自分自身を実装することで、応答に送信されたデータを修正 する機会を持っています.pipe() メソッド。

だからあなたLoggingStreamが

VARストリーム=新しいunion.ResponseStream()した場合例えば、ストリーム(「データ」、 関数(req){console.log({res:this.res.statusCode、メソッド: this.req.method});}); return stream;

console.logステートメントは、期待どおりの順序で起動します。 Node.jsの中でストリームの最大オグデンの最近のブログ記事を読んで、彼らがどのように動作するかすると便利かもしれません:http://maxogden.com/node-streams

乾杯、チャーリー

あなたがより多くの質問がある場合、あなたはgithubの上で私たちを見つけることができます、irc(#nodejitsu on freenode)、メーリングリスト([email protected]) :)

+0

会話をご覧ください:https://groups.google.com/forum/?fromgroups#!topic/flatironjs/9EFr2YWx8xY – Zhami

関連する問題