【问题标题】:How to handle http status codes in express from http post response and pass along to the angularjs controller如何处理来自http post响应的http状态码并传递给angularjs控制器
【发布时间】:2019-06-08 00:33:36
【问题描述】:

我正在尝试处理来自我的 node/express 的各种 http 状态代码并将响应传递回 angular。我收到一个错误,即在将标头发送到客户端后无法设置标头。我如何根据响应处理这个问题并在 nodejs 中记录各种 http 代码?

app.post("/employees", function(req,res) {

    var ServerOptions = {
        method: 'POST',
        uri: 'http://localhost:5001/api/empData',
        body: req.body,
        json: true,
        headers: {
           'Content-Type': 'application/json'
        }
    };
    request(ServerOptions).then(function (Response) {
        res.status(200).json(response);
    })
    .catch(function (err) {
        res.status(401).json({message: "unauthorized"});
        console.log("Unauthorized");
        res.status(404 || 500).json({message: "error"});
        console.log("Error");
        res.set("Connection", "close");
    });

});

【问题讨论】:

  • 请求函数返回什么??请给我们完整的代码或者请描述请求函数是做什么的??

标签: node.js express mean-stack


【解决方案1】:

根据 $http 上的 angularjs 文档,当服务器返回 200 以外的状态时,将调用“错误”回调。

尝试将处理 401 状态的代码放在 .error() 中。

【讨论】:

    猜你喜欢
    • 2017-02-25
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    相关资源
    最近更新 更多