gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:48:10 31 4
gpt4 key购买 nike

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

我看到了this post ,我觉得没有帮助。

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);

最佳答案

查看 the example from enable-cors.org :

In your ExpressJS app on node.js, do the following with your routes:

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) 应该在您应用中的所有其他路由(或至少是您希望启用 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();
});

我用您的代码对此进行了测试,并从 public 目录中获取了 Assets 的 header :

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);

当然,您可以将函数打包到一个模块中,这样您就可以执行类似的操作

// 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());

关于node.js - 如何在 node.js 上的 express.js 框架中启用跨域资源共享 (CORS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11181546/

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