在节点js中发送POST请求的json响应?

时间:2017-03-27 07:21:46

标签: javascript json node.js

我正在尝试在节点js中编写Web服务。因此,基本上当有人通过POST调用我的服务并将相应的参数作为JSON传递时,我的服务应该接收数据,调用flutterwave API并获得响应并将响应发送回JSON中的客户端。除了以JSON格式发送响应之外,我已经能够完成其中的大部分工作。所以,我能够在我的控制台中打印响应,但我想以JSON格式将响应发送到客户端。我怎么做 ?

app.post('/pay', function (req, res) {
    var jsonString = '';
    var validateoption = req.body.validateoption;
    var authmodel = req.body.authmodel;
    var cardno = req.body.cardno;
    var cvv = req.body.cvv;
    var expirymonth = req.body.expirymonth;
    var expiryyear = req.body.expiryyear;

console.log("Validateoption",req.body.validateoption);
console.log("id",req.body.id);


   /* req.on('end', function () {
        console.log(JSON.parse(jsonString));
    });*/
    flutterwave.Card.tokenize({
        "validateoption":validateoption,
        "authmodel":authmodel, /*Only NOAUTH and BVN are accepted*/
        "bvn": "(Optional:Only needed where authmodel is BVN)",
        "cardno":cardno,
        "cvv":cvv,
        "expirymonth":expirymonth,
        "expiryyear":expiryyear

    }, function(err,body)

    {

    });
});

我从Flutterwave获得的响应是​​JSON格式,我想将JSON响应发送回我的客户端。这是我收到的

 body:
  { data:
     { responsecode: '00',
       redirecturl: null,
       avsresponsemessage: null,
       avsresponsecode: null,
       responsemessage: 'Completed Successfully',
       otptransactionidentifier: null,
       transactionreference: null,
       responsehtml: null,
       responsetoken: 'YtUyf18D8l06GloAUTH' },
    status: 'success' },
 flutterwaveRequestSuccessful: true,
}

1 个答案:

答案 0 :(得分:2)

您可以使用res.json()

function(err,body){
   res.json(body.data);
});