简体   繁体   中英

Node.js connecting to remote server from node server

I have a little Node.js application that I'd like to hit a remote API to get some date (ie twitter, microsoft's translation service, weather). From the docs for Node's http I tried putting the sample code in my node server app:

var options = {
  host: 'www.google.com',
  port: 80,
  path: '/index.html'
};

http.get(options, function(res) {
  console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
  console.log("Got error: " + e.message);
});

I've tried it with the google URL from the docs, a twitter API URL and several others, however the only response I can get is:

Got error: connect ECONNREFUSED

Using the same url with wget gives satisfaction, so I know the URL is valid. What do I need to change to get a successful connection from my app server to a remote service?

OR

Some help on how I can debug this would be even better. Then only information I can seem to get from the failure is ECONNREFUSED . I tried using socket.io to forward the error response to the browser (inside .on('error', function(e){...} ) so I could inspect it with webkit. Unfortunately the error object only contains:

Object
    error: Object
        code: "ECONNREFUSED"
        errno: "ECONNREFUSED"
        syscall: "connect"
    __proto__: Object
__proto__: Object

Not really helpful for determining what went wrong.


For reference my entire app.js is:

var express = require('express') , http = require('http') , routes = require('./routes');

var app = module.exports = express.createServer();
var io = require('socket.io').listen(app)

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'ejs');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
  app.use(express.errorHandler());
});

app.get('/', function(req, res){
  routes.index(req, res);
  console.log(req.query);

  var options = {
    host: 'www.google.com',
    port: 80,
    path: '/index.html'
  };

  http.get(options, function(res) {
    console.log("Got response: " + res.statusCode);
  }).on('error', function(e) {
    console.log("Got error: " + e.message);
  });
});

app.listen(4000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);

Me: "Did you make sure that're sending the request through the proxy that's setup for your internet connection?"

me: "no"

Me: "do that"

me: "OK, now it works"

  var options = {
    host: '<proxyip>',
    port: <proxyport>,
    path: 'http://www.google.com/index.html'
  };

  http.get(options, function(res) {
    console.log("Got response: " + res.statusCode);
  }).on('error', function(e) {
    console.log("Got error: " + e.message);
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM