2013-02-13 68 views
0

如果我想將回調函數中的結果寫入響應,但是我無法訪問函數中的res變量,也無法訪問函數外部的結果。如何在javascript中獲取回調函數內的結果?

那麼如何傳遞內外之間的價值?

var http = require('http'); 
var url = require('url'); 

http.createServer 
(
    function (req, res) 
    { 
     var output=''; 
     res.writeHead(200, {'Content-Type': 'text/plain'}); 


     //The url we want is: 'www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new' 
     var options = { 
      host: 'www.random.org', 
      path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new' 
     }; 

     callback = function(response) { 
      var str = ''; 

      //another chunk of data has been recieved, so append it to `str` 
      response.on('data', function (chunk) { 
      str += chunk; 
      }); 

      //the whole response has been recieved, so we just print it out here 
      response.on('end', function() { 
      console.log(str); 

      //output+=str; //get result 
      }); 
     } 

     http.request(options, callback).end(); 


     //output+=str; //get result 
     res.end(output); 
    } 
).listen(80, '127.0.0.1'); 

VER2

var http = require('http'); 
var url = require('url'); 

http.createServer 
(
    function (req, res) 
    { 
     var output=''; 
     res.writeHead(200, {'Content-Type': 'text/plain'}); 


     //The url we want is: 'www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new' 
     var options = { 
      host: 'www.random.org', 
      path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new' 
     }; 

     callback = function(response) { 
      var str = ''; 

      //another chunk of data has been recieved, so append it to `str` 
      response.on('data', function (chunk) { 
      str += chunk; 
      }); 

      //the whole response has been recieved, so we just print it out here 
      response.on('end', function() { 
      res.write('inside'); 
      }); 
     } 

     http.request(options, callback).end(); 

     res.write('outside'); 
     res.end(output); 
    } 
).listen(80, '127.0.0.1'); 

VER3

var http = require('http'); 
var url = require('url'); 

http.createServer 
(
    function (req, res) 
    { 
     res.writeHead(200, {'Content-Type': 'text/plain'}); 


     //The url we want is: 'www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new' 
     var options = { 
      host: 'www.random.org', 
      path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new' 
     }; 

     callback = function(res) { 
      var str = ''; 

      //another chunk of data has been recieved, so append it to `str` 
      res.on('data', function (chunk) { 
      str += chunk; 
      }); 

      //the whole response has been recieved, so we just print it out here 
      res.on('end', function() { 
      res.write('inside'); 
      //or 
      this.write('inside'); 
      }); 
     } 

     http.request(options, callback).end(); 


     res.write('outside'); 
     res.end(); 
    } 
).listen(80, '127.0.0.1'); 

ver4

var http = require('http'); 
var url = require('url'); 

http.createServer 
(
    function (req, res) 
    { 
     res.writeHead(200, {'Content-Type': 'text/plain'}); 


     //The url we want is: 'www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new' 
     var options = { 
      host: 'www.random.org', 
      path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new' 
     }; 

     callback = function(response) { 
      var str = ''; 

      //another chunk of data has been recieved, so append it to `str` 
      response.on('data', function (chunk) { 
      str += chunk; 
      }); 

      //the whole response has been recieved, so we just print it out here 
      response.on('end', function() { 
      res.write('inside'); 
      }); 
     } 

     http.request(options, callback); 


     res.write('outside'); 
     res.end(); 
    } 
).listen(80, '127.0.0.1'); 
+0

我對非建設性評論感到抱歉,但是如何才能在javascript上監聽端口? 'node.js'是否使用一些隱藏的Java小程序來實現? – 2013-02-13 14:18:47

+0

@TomášZato:節點在服務器端運行 – Amberlamps 2013-02-13 14:20:56

+0

啊,哇。所以瀏覽器不會執行這個操作。 Javascript中的服務器?接下來發生什麼?操作系統在JavaScript ...?不管怎樣,謝謝你。 – 2013-02-13 14:24:45

回答

0

你不行。設置回調函數將在回調執行前完成並返回。如果沒有,那麼回調就不需要了。

在回調本身中做任何你需要做的工作,而不是在父功能中。

I can't access the res variable in the function

您應該可以。它仍然在範圍內。

(雖然如果您在回調運行之前調用end(),那麼應該會發生破損)。

+0

在版本2中,我添加了res.write('inside')和res.write('outside'),並且輸出只是「外部」,這個測試顯示res超出了範圍,只有父項函數可以輸出到響應,如果不是在父函數中,我不知道該怎麼辦。 – 2013-02-13 16:57:46

+0

@CLSo - 再次閱讀答案的最後一行。 – Quentin 2013-02-13 16:59:44

+0

...和測試版本2,該行所暗示的變化,它的工作原理。 – Quentin 2013-02-13 17:01:22

相關問題