如何将响应数据从子响应传递到父响应

时间:2020-09-06 20:05:31

标签: javascript node.js express

我需要父级post方法中子级的响应。 Promise或回调有什么办法吗?

const cardRoutes = (app, logger) => {
    const request = require('request');

    // CREATE
    app.post('/payment', (req, res) => {
       
        var currencyCode = req.body.currencyCode;
        var amount = req.body.amount;
        try {
             
            const options = {
                url: 'www.testsite.com',
                json: true,
                headers:{'content-type' : 'application/json'},
                body: {       
                    amount: amount,
                    currency: currencyCode                 
                }
            };           
            request.post(options, (err, res, body) => {
                if (err) {
                    return console.log(err);
                }
                 console.log(res.body.transactionId);
                console.log(`Status: ${res.statusCode}`);
                res.end();
            });
          
            res.statusCode = 200;
            res.json({message:"okay message"});
           
        } catch (e) {
            logger.error("Error: " + e.message);
            res.end();
            return;
        }
    });
};

module.exports = cardRoutes;

0 个答案:

没有答案