The query always returns a null-error object

I use Request and standard Node.js HTTP to write a simple rest service.

Currently, the server-side request handler is as follows:

response.writeHead("400","Nope");
response.end();

And the client-side code is as follows:

request.get(href,handleResponse);
function handleResponse(error,response,body) {

   console.log(arguments);
}

response.statusCode 400but erroralways null.

What do I need to do on the server side to get the Request module to recognize that the response is an error?

+4
source share
2 answers

. , , . : - ( DNS, TCP HTTP), "".

→ ( ) →

→ ( ) →

+5

. , , request, . :

function(connError, httpResponse, body) {
    let error = null;
    if(connError) {
        error = connError;
    } else if(httpResponse.statusCode != 200) {
        error = body;
        body = null;
    }
    finalCallback(error, body);
}
0
source

All Articles