FFF
FFF

Reputation: 791

Node Js/Expresss api call using information from client

So I am writing a webapp to get information from the steam api and I had some cors issues, so I wrote a node/express server to do the actual api call and then the client-side app gets the json off my server. This is working, but only if the api call is hard coded on my server. How can I supply the server with a userid from the "sid" form field from my client?

Server code:

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); 

Client code:

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"));
            }
        });
    });
}

Upvotes: 0

Views: 273

Answers (2)

adeneo
adeneo

Reputation: 318162

The second argument for $.getJSON is data you can send to the server, like any ajax call.

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

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

and catch it in 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);
});

Upvotes: 1

Alexander
Alexander

Reputation: 2297

Try this:

Server code:

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); 

Client code:

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"));
            }
        });
    });
}   

Upvotes: 2

Related Questions