【问题标题】:How can I use Json response from one service to post json data in next service in Nodejs?如何使用来自一个服务的 Json 响应在 Nodejs 的下一个服务中发布 json 数据?
【发布时间】:2017-11-30 03:02:28
【问题描述】:

我正在使用 NodeJs 和 http 模块来测试我的 Web 服务(Restful API)。有4个服务。第一个服务提供一个 json 响应,我需要为第二个服务创建有效负载。如何将响应保存在变量中以便我可以在下一个请求中使用它?

function callback(error, response, body) {
if (!error) {
    var info = JSON.parse(JSON.stringify(body));
    console.log(info);
}
else {
    console.log('Error happened: '+ error);
}

}

//sending request request(options, callback);

【问题讨论】:

  • 到目前为止你尝试过什么?有代码示例吗?

标签: json node.js web-services


【解决方案1】:

一些抽象问题的抽象代码

service1.getData()
.then((result) => {
    return service2.getData(result);
})
.then((result) => {
    return service3.getData(result);
})
...
.then((result) => {
    // do something with the last result
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 2011-01-21
    相关资源
    最近更新 更多