Node.js Requests returning 301 redirects

dxu picture dxu · Jun 16, 2012 · Viewed 23.5k times · Source

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!

Answer

kendotwill picture kendotwill · Dec 5, 2018

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

require('http');

to

require('https');