gpt4 book ai didi

cors - grunt-contrib-connect 中间件 CORS 解决方案,具有 keepalive true

转载 作者:行者123 更新时间:2023-12-03 03:41:47 38 4
gpt4 key购买 nike

对于我的本地开发系统,我尝试使用 grunt-contrib-connect 为前端 Assets 提供服务。我需要一个在 Firefox 中使用字体的跨域解决方案。服务器运行得很好,但我似乎无法设置 header 。

我使用的是 grunt-contrib-connect 0.7.1 版本。

connect: {
dev: {
options: {
port: '9001',
base: 'build',
hostname: 'localhost',
keepalive: true,
middleware: function(connect, options, middlewares) {
// inject a custom middleware into the array of default middlewares
// this is likely the easiest way for other grunt plugins to
// extend the behavior of grunt-contrib-connect
middlewares.push(function(req, res, next) {
req.setHeader('Access-Control-Allow-Origin', '*');
req.setHeader('Access-Control-Allow-Methods', '*');
return next();
});

return middlewares;
}
}
}
}

通过中间件使用 keepalive 是否有问题?

最佳答案

很遗憾没有人早些时候对此做出回应。

您的代码看起来与文档中的一样,但您将 header 添加到 req 而不是 res

第二个问题是文档误导您 (fixed)使用 .push 添加中间件。您的代码根本没有被调用,因为它之前的某些操作正在执行 res.end 和/或没有调用 next()

您的固定代码如下所示:

    middleware: function (connect, options, middlewares) {
// inject a custom middleware
middlewares.unshift(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', '*');
//a console.log('foo') here is helpful to see if it runs
return next();
});

return middlewares;
}

关于cors - grunt-contrib-connect 中间件 CORS 解决方案,具有 keepalive true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22992931/

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