Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 226
dxu Avatar asked Jun 16 '12 05:06

dxu


People also ask

Why is my 301 moved permanently?

The HyperText Transfer Protocol (HTTP) 301 Moved Permanently redirect status response code indicates that the requested resource has been definitively moved to the URL given by the Location headers. A browser redirects to the new URL and search engines update their links to the resource.

Can you reverse a 301 redirect?

Even when used legitimately, 301 redirects are obviously hazardous, as there's no way to undo a permanent redirect once it's been cached by a client. This tells clients that they can remember the redirect for at most one hour, allowing us to change it relatively easily at some point in the future.

What's the method to perform a 301 redirect in JS?

301 redirect using a server-side directive If you need to redirect because the current URL is old, and move the a new URL, it's best to use server-level directive and set the 301 HTTP code to signal search engines that the current URL has permanently moved to the new resource.


1 Answers

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

like image 188
Brad Avatar answered Sep 27 '22 16:09

Brad