http获取NodeJS如何获取错误状态码? [英] http get NodeJS how to get error status code?

查看:983
本文介绍了http获取NodeJS如何获取错误状态码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我必须密集,因为在使用Node.JS http.get或http.request时我无法找到任何地方如何获取错误状态代码。我的代码:

OK, I must be dense since I cannot find anywhere 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);
    });

在req.on位中,我想要的是http状态代码(即401, 403等)。我得到的是一个半无用的错误对象,它不会给我代码或对响应对象的任何引用。我试过拦截函数(响应)回调,但是当有404时,它永远不会被调用。

In that "req.on" bit, what I want is the http status code (i.e. 401, 403, etc.). What I get is a semi-useless 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.

谢谢!

推荐答案

无论来自服务器的响应状态代码如何调用您的回调,因此在回调中,请检查 response.statusCode 。也就是说,4xx状态代码不是您正在处理的级别的错误;服务器响应,只是服务器回应说资源不可用(等等)

Your callback gets called regardless of the response status code from the server, so within your callback, check response.statusCode. That is, a 4xx status code isn't an error at the level you're working at; the server responded, it's just that the server responded by saying the resource wasn't available (etc.)

这是,但特征模糊。以下是他们提供的示例,注释指向相关位:

This is in the documentation but characteristically vague. Here's the example they give, with a comment pointing to the relevant bit:

var https = require('https');

https.get('https://encrypted.google.com/', function(res) {
  console.log("statusCode: ", res.statusCode); // <======= Here's the status code
  console.log("headers: ", res.headers);

  res.on('data', function(d) {
    process.stdout.write(d);
  });

}).on('error', function(e) {
  console.error(e);
});

如果您尝试使用(比如说)未知资源,您会看到 statusCode:404

If you try that with (say) an unknown resource, you'll see statusCode: 404.

所以对于你正在做的事情,你可能会想要这样的事情:

So for what you're doing, you may want something like this:

var deferred = $q.defer();

var req = https.get(options, function (response) {
    var str = '';

    if (response.statusCode < 200 || response.statusCode > 299) { // (I don't know if the 3xx responses come here, if so you'll want to handle them appropriately
        deferred.reject(/*...with appropriate information, including statusCode if you like...*/);
    }
    else {
        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(/*...with appropriate information, but status code is irrelevant [there isn't one]...*/);
});

这篇关于http获取NodeJS如何获取错误状态码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆