如何在node.js的express.js框架中启用跨源资源共享(CORS)

我正在尝试在node.js中构build一个支持跨域脚本的Web服务器,同时仍然从公共目录提供静态文件。 我正在使用express.js,并不确定如何允许跨域脚本( Access-Control-Allow-Origin: * )。

我看到这个post ,我没有find有用的。

 var express = require('express') , app = express.createServer(); app.get('/', function (req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "X-Requested-With"); next(); }); app.configure(function () { app.use(express.methodOverride()); app.use(express.bodyParser()); app.use(app.router); }); app.configure('development', function () { app.use(express.static(__dirname + '/public')); app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); }); app.configure('production', function () { var oneYear = 31557600000; // app.use(express.static(__dirname + '/public', { maxAge: oneYear })); app.use(express.static(__dirname + '/public')); app.use(express.errorHandler()); }); app.listen(8888); console.log('express running at http://localhost:%d', 8888); 

查看来自enable-cors.org的示例 :

在node.js的ExpressJS应用程序中,使用路线执行以下操作:

 app.all('/', function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "X-Requested-With"); next(); }); app.get('/', function(req, res, next) { // Handle the get for this route }); app.post('/', function(req, res, next) { // Handle the post for this route }); 

第一个调用( app.all )应该在应用中的所有其他路由之前(或者至less是您希望启用CORS的路由)。

[编辑]

如果你希望头文件也显示为静态文件,试试这个(确保它在调用之前use(express.static())

 app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "X-Requested-With"); next(); }); 

我用你的代码testing了这个,并从public目录获取资源的头文件:

 var express = require('express') , app = express.createServer(); app.configure(function () { app.use(express.methodOverride()); app.use(express.bodyParser()); app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "X-Requested-With"); next(); }); app.use(app.router); }); app.configure('development', function () { app.use(express.static(__dirname + '/public')); app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); }); app.configure('production', function () { app.use(express.static(__dirname + '/public')); app.use(express.errorHandler()); }); app.listen(8888); console.log('express running at http://localhost:%d', 8888); 

当然,你可以把这个function打包成一个模块,这样你就可以做类似的事情

 // cors.js module.exports = function() { return function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "X-Requested-With"); next(); }; } // server.js cors = require('./cors'); app.use(cors()); 

在@Brandon Tilley解决scheme之后,显然它起初并不适合我。 不知道为什么,也许我使用铬和不同版本的节点。 之后做了一些小的调整,现在正在为我工​​作。

 app.all('*', function(req, res, next) { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS'); res.header('Access-Control-Allow-Headers', 'Content-Type'); next(); }); 

如果遇到类似的问题,可能会有所帮助。

尝试这个Cors NPM模块。

 var cors = require('cors') var app = express() app.use(cors()) 

这个模块提供了很多function来微调cors的设置,比如域名白名单,为特定的apis启用cors等。

我使用这个:

 var app = express(); app .use(function(req, res, next){ res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Headers', 'X-Requested-With'); next(); }) .options('*', function(req, res, next){ res.end(); }) ; h.readFiles('controllers').forEach(function(file){ require('./controllers/' + file)(app); }) ; app.listen(port); console.log('server listening on port ' + port); 

此代码假定您的控制器位于控制器目录中。 这个目录中的每个文件应该是这样的:

 module.exports = function(app){ app.get('/', function(req, res, next){ res.end('hi'); }); } 

推荐使用cors express模块​​。 这允许您将域名列入白名单,允许/限制特定于路由的域等,

我需要做的另一个步骤是将我的URL从http://localhost切换到http://127.0.0.0