0

I'm writing a server in Node.js. I've been using the send-data/json package to send responses, with much success. For example, at the end of my API call I use this code:

sendJson(res, res, {some: content})

This works great. However, I have a need to implement a URL redirect in one of my API endpoints. The code I am seeing everywhere to do this looks like this:

res.writeHead(302, {Location: 'http://myUrl.com'})
res.end()

However, this change causes my server to stop sending responses on this endpoint.

What am I missing?

Note: this is vanilla Node without Express.

Update: to clarify, based on contributions in the comments, this is the result of a GET request, so I expect that redirects should be enabled by default. I am still curious why no response is coming back from the server at all, regardless of whether it is an erroneous redirect attempt or a successful one.

Steverino
  • 2,099
  • 6
  • 26
  • 50
  • Check the answers in this thread and let me know if it fixes your issue: https://stackoverflow.com/questions/32540232/follow-redirect-with-node-js-request/32540337 – MEDZ Nov 01 '19 at 17:10
  • This does not seem to help. I've updated my question with further detail. First of all, I'm already using the GET HTTP method, so according to this answer, `followAllRedirects` should already be on by default. Secondly, the client would have no opportunity to follow redirects or not, because no response is coming out of the server whatsoever. It is not a matter of a bad response, but rather that no response is received at all. – Steverino Nov 01 '19 at 19:35

0 Answers0