我正在尝试使用ExpressJS提供Angular应用程序。这是我的index.js:
var express = require('express');
var path = require('path');
var app = express();
app.set('port', (process.env.PORT || 8000));
app.use(express.static(__dirname));
app.use(express.static(path.join(__dirname + 'fake_data')));
// views is directory for all template files
app.set('views', __dirname + '/app/pages');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.get('/', function(request, response) {
response.render('index');
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
在我的角度应用程序中,还有一个包含API服务器地址的常量:
(function () {
angular.module('Bankbox')
.constant('webConfig', {
API_SERVER: ''
});
})();
在运行阶段设置:
(function () {
angular.module('Bankbox')
.config(bankboxConfig)
.run(runConfig);
function runConfig($location, webConfig){
var port = ":" + $location.port() + "/";
webConfig.API_SERVER = $location.host();
if (port){
webConfig.API_SERVER += port;
}
}
.....
最后,当我执行node index.js
并尝试在浏览器中加载“localhost:8000”时,我收到错误:
XMLHttpRequest cannot load localhost:8000/fake_data/my-deals.json. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
GET请求在工厂中,它看起来像:
function getMyDeals() {
return $http.get(webConfig.API_SERVER + 'fake_data/my-deals.json')
.then($http.getDataFromResult);
}
为什么会这样?我假设localhost:8000不应该把自己想象成另一个起源。