• <legend id='ttn5l'><style id='ttn5l'><dir id='ttn5l'><q id='ttn5l'></q></dir></style></legend>
      <i id='ttn5l'><tr id='ttn5l'><dt id='ttn5l'><q id='ttn5l'><span id='ttn5l'><b id='ttn5l'><form id='ttn5l'><ins id='ttn5l'></ins><ul id='ttn5l'></ul><sub id='ttn5l'></sub></form><legend id='ttn5l'></legend><bdo id='ttn5l'><pre id='ttn5l'><center id='ttn5l'></center></pre></bdo></b><th id='ttn5l'></th></span></q></dt></tr></i><div id='ttn5l'><tfoot id='ttn5l'></tfoot><dl id='ttn5l'><fieldset id='ttn5l'></fieldset></dl></div>

      <small id='ttn5l'></small><noframes id='ttn5l'>

      1. <tfoot id='ttn5l'></tfoot>
          <bdo id='ttn5l'></bdo><ul id='ttn5l'></ul>

        node.js http.request 事件流 - 我的 END 事件去哪儿了?

        node.js http.request event flow - where did my END event go?(node.js http.request 事件流 - 我的 END 事件去哪儿了?)

        <small id='In9WD'></small><noframes id='In9WD'>

        <tfoot id='In9WD'></tfoot>

        1. <i id='In9WD'><tr id='In9WD'><dt id='In9WD'><q id='In9WD'><span id='In9WD'><b id='In9WD'><form id='In9WD'><ins id='In9WD'></ins><ul id='In9WD'></ul><sub id='In9WD'></sub></form><legend id='In9WD'></legend><bdo id='In9WD'><pre id='In9WD'><center id='In9WD'></center></pre></bdo></b><th id='In9WD'></th></span></q></dt></tr></i><div id='In9WD'><tfoot id='In9WD'></tfoot><dl id='In9WD'><fieldset id='In9WD'></fieldset></dl></div>
        2. <legend id='In9WD'><style id='In9WD'><dir id='In9WD'><q id='In9WD'></q></dir></style></legend>

              <bdo id='In9WD'></bdo><ul id='In9WD'></ul>

                <tbody id='In9WD'></tbody>
                • 本文介绍了node.js http.request 事件流 - 我的 END 事件去哪儿了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I am working on a cunning plan that involves using node.js as a proxy server in front of another service.

                  In short:

                  1. Dispatch incoming request to a static file (if it exists)
                  2. Otherwise, dispatch the request to another service

                  I have the basics working, but now attempting to get the whole thing working with Sencha Connect so I can access all the kick-ass middleware provided.

                  All of the action happens in dispatchProxy below

                  connect(
                    connect.logger(), 
                    connect.static(__dirname + '/public'),
                    (request, response) ->  
                      dispatchProxy(request, response)
                  ).listen(8000)
                  
                  dispatchProxy = (request, response) ->  
                  
                    options = {host: host, port: port, method: request.method, headers: request.headers, path: request.url}
                  
                    proxyRequest = http.request(options, (proxyResponse) ->
                      proxyResponse.on('data', (chunk) ->
                       response.write(chunk, 'binary')
                      )
                  
                      proxyResponse.on('end', (chunk) ->        
                       response.end()
                      )
                  
                      response.writeHead proxyResponse.statusCode, proxyResponse.headers    
                    )
                  
                    request.on('data', (chunk) ->
                      proxyRequest.write(chunk, 'binary')
                    )
                  
                    # this is never triggered for GETs
                    request.on('end', ->
                      proxyRequest.end()
                    )
                  
                    # so I have to have this here
                    proxyRequest.end()
                  

                  You will notice proxyRequest.end() on the final line above.

                  What I have found is that when handling GET requests, the END event of the request is never triggered and therefore a call to proxyRequest.end() is required. POST requests trigger both DATA and END events as expected.

                  So several questions:

                  • Is this call to proxyRequest.end() safe? That is, will the proxyResponse still be completed even if this is called outside of the event loops?

                  • Is it normal for GET to not trigger END events, or is the END being captured somewhere in the connect stack?

                  解决方案

                  The problem is less the end event and more the data event. If a client makes a GET requests, there's headers and no data. This is different from POST, where the requester is sending data, so the on("data") handler gets hit. So (forgive me for the JS example, I'm not that familiar with coffeescript):

                  var http = require('http');
                  
                  // You won't see the output of request.on("data")
                  http.createServer(function (request, response) {
                    request.on("end", function(){
                      console.log("here");
                    });
                    request.on("data", function(data) {
                      console.log("I am here");
                      console.log(data.toString("utf8"));
                    });
                    response.writeHead(200, {'Content-Type': 'text/plain'});
                    response.end('Hello World
                  ');
                  }).listen(8124);
                  
                  console.log('Server running at http://127.0.0.1:8124/');
                  

                  If I make a curl call to this server, the data event never gets hit, because the GET request is nothing more than headers. Because of this, your logic becomes:

                  // okay setup the request...
                  // However, the callback doesn't get hit until you
                  // start writing some data or ending the proxyRequest!
                  proxyRequest = http.request(options, (proxyResponse) ->
                    // So this doesn't get hit yet...
                    proxyResponse.on('data', (chunk) ->
                     response.write(chunk, 'binary')
                    )
                  
                    // and this doesn't get hit yet
                    proxyResponse.on('end', (chunk) ->
                     // which is why your response.on("end") event isn't getting hit yet        
                     response.end()
                    )
                  
                    response.writeHead proxyResponse.statusCode, proxyResponse.headers    
                  )
                  
                  // This doesn't get hit!
                  request.on('data', (chunk) ->
                    proxyRequest.write(chunk, 'binary')
                  )
                  
                  // So this isn't going to happen until your proxyRequest
                  // callback handler gets hit, which hasn't happened because
                  // unlike POST there's no data in your GET request
                  request.on('end', ->
                    proxyRequest.end()
                  )
                  
                  // now the proxy request call is finally made, which
                  // triggers the callback function in your http request setup
                  proxyRequest.end()
                  

                  So yes you're going to have to manually call proxyRequest.end() for GET requests due to the logic branching I just mentioned.

                  这篇关于node.js http.request 事件流 - 我的 END 事件去哪儿了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
                  How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社
                  How to use coffeescript in developing web-sites?(如何在开发网站时使用coffeescript?)

                • <i id='TotPQ'><tr id='TotPQ'><dt id='TotPQ'><q id='TotPQ'><span id='TotPQ'><b id='TotPQ'><form id='TotPQ'><ins id='TotPQ'></ins><ul id='TotPQ'></ul><sub id='TotPQ'></sub></form><legend id='TotPQ'></legend><bdo id='TotPQ'><pre id='TotPQ'><center id='TotPQ'></center></pre></bdo></b><th id='TotPQ'></th></span></q></dt></tr></i><div id='TotPQ'><tfoot id='TotPQ'></tfoot><dl id='TotPQ'><fieldset id='TotPQ'></fieldset></dl></div>
                • <small id='TotPQ'></small><noframes id='TotPQ'>

                  1. <tfoot id='TotPQ'></tfoot>

                    <legend id='TotPQ'><style id='TotPQ'><dir id='TotPQ'><q id='TotPQ'></q></dir></style></legend>
                      <bdo id='TotPQ'></bdo><ul id='TotPQ'></ul>

                          <tbody id='TotPQ'></tbody>