Node.js 节点rest客户端:发送标头后无法设置标头。

Node.js 节点rest客户端:发送标头后无法设置标头。,node.js,node-rest-client,Node.js,Node Rest Client,我正在尝试使用节点REST客户端从节点调用RESTAPI。如果我的呼叫返回错误,我希望捕获错误并将其报告给呼叫方 我正在尝试与邮递员,不幸的是,这只工作一次。当我第二次按send时,我的node.js程序崩溃,错误是发送后无法设置头 我是node.js的新手,非常感谢您的帮助 //app stuff const client_id = "x"; const client_secret = "y"; const callback = "https://myurl; // Basic Setup

我正在尝试使用节点REST客户端从节点调用RESTAPI。如果我的呼叫返回错误,我希望捕获错误并将其报告给呼叫方

我正在尝试与邮递员,不幸的是,这只工作一次。当我第二次按send时,我的node.js程序崩溃,错误是发送后无法设置头

我是node.js的新手,非常感谢您的帮助

//app stuff
const client_id = "x";
const client_secret = "y";
const callback = "https://myurl;

// Basic Setup
var http     = require('http'),
    express  = require('express'),
    mysql    = require('mysql'),
    parser   = require('body-parser'),
    Client   = require('node-rest-client').Client;

var client = new Client();

// Setup express
var app = express();
app.use(parser.json());
app.use(parser.urlencoded({ extended: true }));
app.set('port', process.env.PORT || 5000);

// Set default route
app.get('/', function (req, res) {
    res.send('<html><body><p>Welcome to Bank API Wrapper</p></body></html>');
});

app.post('/authorize', function (req,res) {
    var response = [];

    if (typeof req.body.code !== 'undefined' && typeof req.body.state !== 'undefined' ){
        var code = req.body.code, state = req.body.state;

        //conversion to base64 because citi api wants it this way
        var authorization = "Basic " + Buffer.from(client_id + ":" + client_secret).toString('base64');

        var args = {
            data:{"grant_type":"authorization_code","code":code,"redirect_uri":callback},
            headers:{"Authorization":authorization,"Content-Type":"application/x-www-form-urlencoded"} 
        };

        //get access and refresh token
        client.post("https://sandbox.apihub.citi.com/gcb/api/authCode/oauth2/token/sg/gcb", args, function (citidata, citiresponse) {
            //console.log(citidata);
            //console.log(citiresponse);
        });

        client.on('error', function (err) {
            response.push({'result' : 'error', 'msg' : 'unauthorized access'});
            res.setHeader('Content-Type', 'application/json');
            res.status(200).send(JSON.stringify(response));
        });
    } 
    else {
        response.push({'result' : 'error', 'msg' : 'Please fill required details'});
        res.setHeader('Content-Type', 'application/json');
        res.status(200).send(JSON.stringify(response));
    }
});


// Create server
http.createServer(app).listen(app.get('port'), function(){
    console.log('Server listening on port ' + app.get('port'));
});
你有这个:

client.on('error', function (err) {
这将在客户端上注册一个错误处理程序,但它永远不会被删除。客户端在请求之间共享,因此后续请求中的任何错误仍将触发旧的错误处理程序

相反,您可以侦听请求中的错误。大概是这样的:

var request = client.post("...

request.on('error', function(err) {
    // handle error
});

请参见

该程序对“请填写所需详细信息”部分没有问题。问题出在写client.on“error”的部分。它只工作一次。在那之后,我得到了一个错误:发送邮件后无法设置邮件头。尝试了一下,得到了这个错误。只有客户端。错误。。。捕捉错误。events.js:165抛出错误;^错误:未捕获、未指定的错误事件。分析响应时出错。答复:[{type:bad request,code:400….我通过移动var client=new client;在app.post中解决了这个问题。谢谢!您关于在请求之间共享客户端的评论非常有用!谢谢!@JacksonNg我自己测试过它,并且使用我描述的请求错误处理程序确实捕获了错误。不需要在ap中移动客户端创建p、 有趣的是,它对我不起作用。似乎client.on发现了错误,但request.on没有。