在角度全栈生成器上启用 CORS
enable CORS on angular-fullstack generator?
好的,我有一个使用 nodeJs 启动并运行的 EC2 实例,并表示我正在使用生成器和我的应用程序的香草安装.js文件如下:
/**
* Main application file
*/
'use strict';
// Set default node environment to development
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var express = require('express');
var mongoose = require('mongoose');
var config = require('./config/environment');
// Connect to database
mongoose.connect(config.mongo.uri, config.mongo.options);
// Populate DB with sample data
//require('./config/seed');
// Setup server
var app = express();
var server = require('http').createServer(app);
var socketio = require('socket.io')(server, {
serveClient: (config.env === 'production') ? false : true,
path: '/socket.io-client'
});
require('./config/socketio')(socketio);
require('./config/express')(app);
require('./routes')(app);
// Start server
server.listen(config.port, config.ip, function () {
console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
});
// Expose app
exports = module.exports = app;
route.js 如 require('./routes')(app) 中所述;如下:
/**
* Main application routes
*/
'use strict';
var errors = require('./components/errors');
var cors = require('./config/cors');
module.exports = function(app) {
/*
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.all('/', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
*/
// Insert routes below
app.use('/api/things', require('./api/thing'));
app.use('/api/users', require('./api/user'));
app.use('/auth', require('./auth'));
// All undefined asset or api routes should return a 404
app.route('/:url(api|auth|components|app|bower_components|assets)/*')
.get(errors[404]);
// All other routes should redirect to the index.html
app.route('/*')
.get(function(req, res) {
res.sendfile(app.get('appPath') + '/index.html');
});
};
express.js 如 require('./express')(app) 中所述;如下所示:
/**
* Express configuration
*/
'use strict';
var express = require('express');
var favicon = require('serve-favicon');
var morgan = require('morgan');
var compression = require('compression');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var cookieParser = require('cookie-parser');
var errorHandler = require('errorhandler');
var path = require('path');
var config = require('./environment');
var passport = require('passport');
module.exports = function(app) {
var env = app.get('env');
app.set('views', config.root + '/server/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(compression());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(methodOverride());
app.use(cookieParser());
app.use(passport.initialize());
if ('production' === env) {
app.use(favicon(path.join(config.root, 'public', 'favicon.ico')));
app.use(express.static(path.join(config.root, 'public')));
app.set('appPath', config.root + '/public');
app.use(morgan('dev'));
}
if ('development' === env || 'test' === env) {
app.use(require('connect-livereload')());
app.use(express.static(path.join(config.root, '.tmp')));
app.use(express.static(path.join(config.root, 'client')));
app.set('appPath', 'client');
app.use(morgan('dev'));
app.use(errorHandler()); // Error handler - has to be last
}
};
但无论我在哪里声明这些:
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.all('/', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
它不起作用,我得到:
XMLHttpRequest cannot load http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.financ…5.NYM%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access.
您需要
在节点快速应用程序中启用 CORS 中间件。您可以通过添加
"cors": "^2.5.1",
到你在package.json和app模块中的依赖项,
var cors = require('cors');
//add cors to do the cross site requests
app.use(cors());
相关文章:
- 在Golang回调函数中启用CORS
- 如何在angular js中启用CORS
- 在JQuery中启用CORS后,CORS请求被阻止
- 如何在OrientDb-server-config.xml中启用CORS
- 无法理解如何使用 ajax 调用启用 CORS
- 在角度全栈生成器上启用 CORS
- 在 Firefox 中启用 Cors
- 如何在Dynamics CRM中启用CORS
- 在NodeJS中启用CORS
- 启用CORS时的安全漏洞
- 流明:启用CORS
- 在API上启用CORS
- 如何启用CORS(JS+MVC Web API)
- 如何在谷歌驱动器上托管的文件夹中启用CORS
- 在Sharepoint 2013中启用CORS
- 快速启用CORs标头仍然会导致错误
- 在WebAPI上启用CORS, Chrome和FF仍然不起作用
- 在Web API Core和Angular JS中启用CORS
- 你好,我需要知道如何在OpenShift的RAILS REST应用程序中启用CORS
- 如何在Phonegap应用程序中使用canvas来启用CORS