How to get error status code of http get NodeJS?

+1 vote

 How to get the error status codes when using Node.JS http.get or http.request.

My code:

var deferred = $q.defer();
var req = https.get(options, function(response) {
  var str = '';
  response.on('data', function(chunk) {
    str += chunk;
  });
  response.on('end', function() {
    console.log("[evfService] Got user info: " + str);
    deferred.resolve(str);
  });
});

req.on('error', function(e) {
  deferred.reject(e);
});

In that "req.on" bit, what I want is the http status code (i.e. 401, 403, etc.).  I got error object that does not give me the code or any reference to the response object.

I have tried intercepting in the function(response) callback, but when there is a 404, it never gets called.

Thanks!

Jun 2 in Java-Script by kartik
• 15,440 points
47 views

1 answer to this question.

0 votes

Hello @kartik,

An error code 400 response is not considered an error by node.js.

Try response.statusCode in this:

request.on('response', function (response) {}); 

Hope this help!!

Thank You!!

answered Jun 2 by Niroj
• 32,200 points

Related Questions In Java-Script

0 votes
1 answer
0 votes
1 answer

How to trigger jquery.ajax() error callback based on server response, not HTTP 500?

Hello @kartik, The error callback will be executed ...READ MORE

answered Jun 18 in Java-Script by Niroj
• 32,200 points
36 views
0 votes
1 answer

How to Scroll to the top of the page using JavaScript?

Hii @kartik, Using javascript <script> $("a[href='#top']").click(function() { ...READ MORE

answered Apr 2 in Java-Script by Niroj
• 32,200 points
18 views
0 votes
1 answer

How to access GET directly from JavaScript?

Hello @kartik, You can first go through: window.location.search It will ...READ MORE

answered Apr 29 in Java-Script by Niroj
• 32,200 points
47 views
0 votes
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 32,200 points
810 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18 in Laravel by Niroj
• 32,200 points
82 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23 in Laravel by Niroj
• 32,200 points
94 views
0 votes
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23 in Laravel by Niroj
• 32,200 points
434 views
0 votes
1 answer

How to get the directory of the currently running file?

Hello @kartik, This should do it: import ( ...READ MORE

answered May 5 in Java-Script by Niroj
• 32,200 points
35 views
0 votes
1 answer

How to get rid of underline for Link component of React Router?

Hello, If you are using styled-components, you could do ...READ MORE

answered May 18 in Java-Script by Niroj
• 32,200 points
157 views