简体   繁体   中英

Node.js Requests returning 301 redirects

I'm brand new to node.js, but I wanted to play around with some basic code and make a few requests. At the moment, I'm playing around with the OCW search ( http://www.ocwsearch.com/ ), and I'm trying to make a few basic requests using their sample search request:

However, no matter what request I try to make (even if I just query google.com), it's returning me

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/0.7.65</center>
</body>
</html>

I'm not too sure what's going on. I've looked up nginx, but most questions asked about it seemed to be asked by people who were setting up their own servers. I've tried using an https request instead, but that returns an error 'ENOTFOUND'.

My code below:

var http = require('http');

http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('Hello World\n');

    var options = {
      host:'ocwsearch.com',
      path:
      '/api/v1/search.json?q=statistics&contact=http%3a%2f%2fwww.ocwsearch.com%2fabout/',
      method: 'GET'
    }  


    var req = http.request(options, function(res) {
      console.log("statusCode: ", res.statusCode);
      console.log("headers: ", res.headers);
      res.on('data', function(d) {
            process.stdout.write(d);
      });
    });
    req.end();

    req.on('error', function(e) {
      console.error(e);
    });


}).listen(8124);

console.log('Server running at http://127.0.0.1:8124/');

Sorry if this is a really simple question, and thanks for any help you can give!

The problem is that Node.JS's HTTP Request module isn't following the redirect you are given.

See this question for more: How do you follow an HTTP Redirect in Node.js?

Basically, you can either look through the headers and handle the redirect yourself, or use one of the handful of modules for this. I've used the "request" library, and have had good luck with it myself. https://github.com/mikeal/request

For me the website I was trying to GET was redirecting me to the secure protocol. So I changed

require('http');

to

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

var find_link = function(link, callback){

  var root =''; 

  var f = function(link){

    http.get(link, function(res) {

      if (res.statusCode == 301) {
        f(res.headers.location);
      } else {
        callback(link);
      } 

    });
 }

  f(link, function(t){i(t,'*')});
}   

find_link('http://somelink.com/mJLsASAK',function(link){
  console.log(link);
});

function i(data){
  console.log( require('util').inspect(data,{depth:null,colors:true}) )
}

This question is old now, but I got the same 301 error and these answers didn't actually help me to solve the problem.

I wrote the same code:

var options = {
    hostname: 'google.com',
    port: 80,
    path: '/',
    method: 'GET',
    headers: {
       'Content-Type': 'text/plain',
    }
};

var http = require('http');

var req = http.request(options, function(res) {
    console.log('STATUS:',res.statusCode);
    console.log('HEADERS: ', JSON.stringify(res.headers));
    res.setEncoding('utf8');

    res.on('data', function(chunk) {
        console.log(chunk);
    });
    res.on('end', function() {
        console.log('No more data in response.');
    });
});

req.on('error', function(e) {
    console.log('problem with request: ', e.message);
});
console.log(req);

req.end();

so after some time I realized that there's a really tiny mistake in this code which is hostname part:

 var options = {
     hostname: 'google.com',
     ...

you have to add "www." before your URL to get html content, otherwise there would be 301 error.

var options = {
     hostname: 'www.google.com',

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