NodeJS http redirect request

To send a redirect response with the plain http module, you just need to manufacture the right type of redirect response and send that as the response. A redirect response has a 3xx status code and sets the location header and would generally not include a body.

res.setHeader('location', 'http://wwww.google.com');
res.statusCode = 302;
res.end();

Keep in mind that a redirect response is just an instruction/suggestion to the client that they should go somewhere else to get the content they requested. It’s up to the client whether it actually follows the redirect or not. A browser will automatically follow redirects in many circumstances. But, an Ajax call using XMLHttpRequest from browser-based Javascript will not automatically follow a redirect. Instead, the script receiving the response would have to examine the status code of 3xx, read the location header and issue a new request to the new http url. Other http client libraries may have features you can enable to automatically follow redirects.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top