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


Question

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?

1
66
2/10/2016 9:32:30 AM

Accepted Answer

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

44
4/24/2014 12:55:18 PM

If all you want to do is follow redirects but still want to use the built-in HTTP and HTTPS modules, I suggest you use https://github.com/follow-redirects/follow-redirects.

yarn add follow-redirects
npm install follow-redirects

All you need to do is replace:

var http = require('http');

with

var http = require('follow-redirects').http;

... and all your requests will automatically follow redirects.

Disclosure: I wrote this module.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon