节点Js / Expresss api使用来自客户端的信息进行呼叫

时间:2015-10-24 21:45:53

标签: javascript node.js api express get

所以我正在编写一个webapp来获取来自steam api的信息,我有一些cors问题,所以我写了一个节点/快速服务器来进行实际的api调用,然后客户端应用程序从我的服务器上获取json 。这是有效的,但只有在我的服务器上硬编码api调用时。如何为服务器提供来自" sid"的用户ID?来自我的客户的表格字段?

服务器代码:

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

var app = express();  
function proxy(){
  app.use('/', function(req, res) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    var apiServerHost = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=[APIKEY]&steamids=";
    var userId = 76561197972495328;
    var url = apiServerHost+userId;
    req.pipe(request(url)).pipe(res);
  });
}

proxy();


app.listen(process.env.PORT || 3000); 

客户代码:

function submit(){
    $(document).ready(function(){
        var sid = document.getElementById("idform").elements[0].value;
        $.getJSON("http://localhost:3000/", function(result){
            console.log(result);
            console.log(result.response.players[0]);
            for(i in result.response.players[0]){
                document.getElementById("main").appendChild(document.createTextNode(i + " : " + result.response.players[0][i]));
                document.getElementById("main").appendChild(document.createElement("br"));
            }
        });
    });
}

2 个答案:

答案 0 :(得分:2)

试试这个:

服务器代码:

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

var app = express();  
function proxy(){
  app.use('/', function(req, res) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    var apiServerHost = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=[APIKEY]=";
    var userId = encodeURIComponent(req.query.sid);
    var url = apiServerHost+userId;
    req.pipe(request(url)).pipe(res);
  });
}

proxy();


app.listen(process.env.PORT || 3000); 

客户代码:

function submit(){
    $(document).ready(function(){
        var sid = document.getElementById("idform").elements[0].value;
        $.getJSON("http://localhost:3000/?sid=" + encodeURIComponent(sid), function(result){
            console.log(result);
            console.log(result.response.players[0]);
            for(i in result.response.players[0]){
                document.getElementById("main").appendChild(document.createTextNode(i + " : " + result.response.players[0][i]));
                document.getElementById("main").appendChild(document.createElement("br"));
            }
        });
    });
}   

答案 1 :(得分:1)

$.getJSON的第二个参数是您可以发送到服务器的数据,就像任何ajax调用一样。

var sid = document.getElementById("idform").elements[0].value;

$.getJSON("http://localhost:3000/", {sid: sid}, function(result){ ...

并在Node

中捕获它
app.use('/', function(req, res) {
    var sid = req.query.sid || "";

    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

    var qs  = "?key=[APIKEY]&steamids=" + sid;
    var url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/" + qs;

    req.pipe(request(url)).pipe(res);
});