gpt4 book ai didi

javascript - 在 angular-fullstack 生成器上启用 CORS?

转载 作者:行者123 更新时间:2023-11-30 08:40:10 28 4
gpt4 key购买 nike

好的,我已经启动了一个 EC2 实例并使用 nodeJs 运行并表示我正在使用 generator 的 Vanilla 安装我的 app.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;

在 require('./routes')(app) 中声明的 route.js;如下:

/**
* 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');
});
};

在 require('./express')(app) 中声明的 express.js;如下:

/**
* 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.

最佳答案

您需要在您的 node-express 应用程序中启用 CORS 中间件。你可以通过添加来做到这一点

"cors": "^2.5.1",

在 package.json 和 app 模块中的依赖项,

var cors = require('cors');
//add cors to do the cross site requests
app.use(cors());

关于javascript - 在 angular-fullstack 生成器上启用 CORS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27336489/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com