【问题标题】:Make request in NodeJs server?在 NodeJs 服务器中发出请求?
【发布时间】:2012-09-10 19:41:59
【问题描述】:

我已经创建了一个服务器 http 监听器:

var http = require('http');
http.createServer(function (req, res)
{
        res.writeHead(200,
        {
                'Content-Type': 'text/plain'
        });
        res.write('aaa');
        res.end();
}).listen(1337, '127.0.0.1');
console.log('waiting......');

它正在查找并响应。

现在,我想要 - foreach 客户端请求 - 服务器执行 another 请求并 追加 一个字符串 "XXX"

所以我写了:

var http = require('http');
var options = {
        host: 'www.random.org',
        path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
};
http.createServer(function (req, res)
{
        res.writeHead(200,
        {
                'Content-Type': 'text/plain'
        });
        res.write('aaa');

        http.request(options, function (r)
        {
                r.on('data', function (chunk)
                {
                        res.write('XXX');
                });
                r.on('end', function ()
                {
                        console.log(str);
                });
                res.end();
        });

        res.end();
}).listen(1337, '127.0.0.1');
console.log('waiting......');

所以现在 foreach 请求应该写成:aaaXXX (aaa+XXX)

但它不起作用。它仍然产生相同的输出。

我哪里错了?

【问题讨论】:

  • 查找 socket.io 或类似 WebSocket 的节点。开箱即用的 Node 仍然只是一个基本的 HTTP 服务器。实时部分来自 WebSockets。
  • @psyketom 但是这个基本的 Http 服务器可以发出他自己的另一个请求。这就是我的问题所在。 :)

标签: javascript node.js


【解决方案1】:

试试这个:

var http = require('http');
var options = {
        host: 'www.random.org',
        path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
};
http.createServer(function (req, res)
{
        res.writeHead(200,
        {
                'Content-Type': 'text/plain'
        });
        res.write('aaa');

        var httpreq = http.request(options, function (r)
        {
            r.setEncoding('utf8');
            r.on('data', function (chunk)
            {
                res.write(' - '+chunk+' - ');
            });
            r.on('end', function (str)
            {
                res.end();
            });

        });

        httpreq.end();

}).listen(1337, '127.0.0.1');
console.log('waiting......');

另外,值得一读this article on nodejitsu

【讨论】:

  • 我有没有告诉过你——我爱你? :-) 非常感谢。它正在工作
  • 首先,我删除了res.end(),因为之前的http.request是异步的,我们应该等待它完成,另一部分是我们需要.end()http.request让它实际执行(我们可以在.end() 之前发布一些数据,这就是我们拥有该方法的原因)
  • 你为什么在r.on('data', function (chunk)上打电话给res.end();
  • @RoyiNamir,好点,我的错..这应该只在.on('end'
【解决方案2】:

你打电话给res.end() 太早了……你只想在所有东西都写好后做事(例如,当 r.on('end') 被调用时)。

对于这样的事情,我会强烈推荐使用优秀的请求库 (https://github.com/mikeal/request)。

这有一个很棒的API,例如:

var request = require('request');
request('http://www.google.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body) // Print the google web page.
  }
})

【讨论】:

  • 仍然,删除end(第一个)后不起作用....为什么他不能发出另一个请求并连接字符串? (我还没有改变你的解决方案。我试图弄清楚为什么我的工作不正常)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-19
  • 2017-08-15
相关资源
最近更新 更多