使用Ember从一个JS服务器向另一个JS服务器提供信息

时间:2016-06-20 17:46:50

标签: ember.js server

我们目前正在使用Steam API进行Ember项目,但发现API不允许跨源请求。因此,我们根据找到的here文档创建了一个服务器。

虽然我们已经创建了一个能够成功调用API并获取我们请求的信息的服务器,但我们发现服务器在端口:4000上运行,我们需要它连接到端口:4200。

我们有办法将信息从此服务器推送到ember服务器吗?任何帮助将不胜感激。

以下是我们用于服务器的代码:

var express = require('express');

var app = express();

app.get('/', function(httpRequest, httpResponse) {
    httpResponse.send('Hello, World!');
});

app.get('/hello-frank', function(httpRequest, httpResponse) {
httpResponse.send('Hello, Frank.');
});

app.post('/hello-frank', function(httpRequest, httpResponse) {
httpResponse.send("No, Frank. You're not allowed to post.");
});

app.get('/hello/:name', function(httpRequest, httpResponse) {
var name = httpRequest.params.name;
httpResponse.send('Hello, ' + name + '!');
});

var request = require('request');

var url = 'http://api.steampowered.com/ISteamUserStats/GetSchemaForGame/' +
'v2/?key=YOURSTEAMAPIKEYHERE&appid=8930';


request.get(url, function(error, steamHttpResponse, steamHttpBody) {
// Print to console to prove we downloaded the achievements.
console.log(steamHttpBody);
});

app.get('/steam/civ5achievements', function(httpRequest, httpResponse) {
// Calculate the Steam API URL we want to use
var url = 'http://api.steampowered.com/ISteamUserStats/GetSchemaForGame/' +
    'v2/?key=YOURSTEAMAPIKEYHERE&appid=8930';
request.get(url, function(error, steamHttpResponse, steamHttpBody) {
    // Once we get the body of the steamHttpResponse, send it to our client
    // as our own httpResponse
    httpResponse.setHeader('Content-Type', 'application/json');
    httpResponse.send(steamHttpBody);
});
});


app.get('/steam/game/:appid/achievements', function(httpRequest, httpResponse) {
// Calculate the Steam API URL we want to use
var url = 'http://api.steampowered.com/ISteamUserStats/GetSchemaForGame/' +
    'v2/?key=YOURSTEAMAPIKEYHERE&appid=' +
    httpRequest.params.appid;
request.get(url, function(error, steamHttpResponse, steamHttpBody) {
    httpResponse.setHeader('Content-Type', 'application/json');
    httpResponse.send(steamHttpBody);
});
});


app.use('/', express.static('public'));


var bodyParser = require('body-parser');

app.use(bodyParser.text());


app.post('/frank-blog', function(httpRequest, httpResponse) {
console.log(httpRequest.body);

httpResponse.status(200).send('Posted today:\n\n' + httpRequest.body);

var port = 4000;
var server = app.listen(port);
console.log('Listening on port ' + port);

1 个答案:

答案 0 :(得分:1)

为了开发目的,您可以使用ember代理功能。而不是像这样启动服务器:

ember s
你是这样做的:

ember s --proxy=http://localhost:4000

因此,假设您的其他服务器位于端口4000上,所有未知的HTTP请求都会重定向到您的服务器。