Search code examples
node.jshttp-redirect

How do you follow an HTTP Redirect in Node.js?


I want to open a page up in node and process the contents in my application. Something like this seems to work well:

var opts = {host: host, path:pathname, port: 80};
http.get(opts, function(res) {
  var page = '';
  res.on('data', function (chunk) {
    page += chunk;
  });
  res.on('end', function() {
     // process page
  });

This doesn't work, however, if the page returns an 301/302 redirect. How would I do that in a reusable way in case there are multiple redirects? Is there a wrapper module on top of the http to more easily handle processing http responses from a node application?


Solution

  • Is there a wrapper module on top of the http to more easily handle processing http responses from a node application?

    request

    Redirection logic in request