Harry Ramli4,738 Points
Http request errors vs status code errors
What is the difference between handling errors using status codes and request.on(‘error’) ? I thought both are related to http get requests and error event runs when status code is anything but 200.
Kevin Gates13,162 Points
Not every non-200 response is an error (list of response codes: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) . For instance, a 201 is a created response. A 301 means something was permanently moved.
Certain errors can occur when you are initially attempting to connect to the server. You may get a 500 error. Other times the server may not respond at all, and the connection attempt will timeout, resulting in an error.
So you need a catch for the error that occurs if the server doesn't respond.
Next, you will need to check to see the type of response that the server does give you. Depending on what you're asking and depending on what the response is, you will then decide if this is merely information (Something was created, 201) or is an error (404, not found).
Hope this helps. :)