Skip to content

Instantly share code, notes, and snippets.

@chapel
Created February 5, 2011 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chapel/812582 to your computer and use it in GitHub Desktop.
Save chapel/812582 to your computer and use it in GitHub Desktop.
var sys = require("sys"),
http = require("http"),
url = require("url"),
querystring = require("querystring");
var apiPort = process.ARGV[2] || 8080;
var apiKey = 'YOUR KEY HERE';
http.createServer(function(req, res) {
var jsonp = 'jsonp';
var params = url.parse(req.url, true).query;
var path = url.parse(req.url, true).pathname;
function writeJSONP(contents) {
res.writeHead(200, {
'Content-Type' : 'application/javascript'
});
res.write(jsonp + '(' + contents + ')');
return res.end();
}
if (typeof params == 'undefined') {
res.writeHead(400);
return res.end();
}
if (typeof params.jsonp != 'undefined' && params.jsonp != '') {
jsonp = (params.jsonp + '').replace(/[^a-zA-Z0-9._$]+/g, '');
}
delete params.jsonp;
params.key = apiKey;
params.format = 'json';
// New 0.3.6+ http api
var options = {
host: 'api.steampowered.com',
port: 80,
path: path + '?' + querystring.stringify(params),
method: 'GET'
};
var request = http.request(options, function(res) {
results = '';
res.on('data', function(chunk) {
results += chunk;
});
res.on('end', function() {
writeJSONP(results);
});
});
request.end();
}).listen(apiPort);
sys.puts('Server running at http://127.0.0.1:' + apiPort);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment