2013-02-13 115 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

相关问题