通过节点/表达/请求管道多个api调用cient

时间:2015-10-25 22:11:33

标签: json node.js api express request

我有一个用于steam api的web应用程序,可以通过单个服务器端api调用正常工作。但我想将两个或更多api调用传递给客户端。目前以下代码有效,如何将second_json发送给客户端?

var express = require('express');
var request = require('request');
var app = express();

function proxy() {
    app.use('/', function(req, res) {
        var sid = req.query.sid[1].trim();
        var url ="http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=[APIKEY]&steamids=";
        var url2 ="http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=[APIKEY]&vanityurl=";
        var url3 =  "http://api.steampowered.com/IPlayerService/GetRecentlyPlayedGames/v0001/?key=[APIKEY]&steamid=";
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept");
        request(url2 + sid, function(error, response, body) {
            var newid = JSON.parse(body).response.steamid
            if (newid !== undefined) {
                sid = newid;
            }
            request(url3+sid+"&format=json", function(err, resp, result){
               var second_json = result;
               req.pipe(request(url + sid)).pipe(res);
            })
        });
    });
}
proxy();
app.listen(process.env.PORT || 3000);

0 个答案:

没有答案