对于我的本地开发系统,我尝试使用 grunt-contrib-connect 为前端资产提供服务。我需要一个在 Firefox 中使用字体的跨域解决方案。服务器运行得很好,但我似乎无法设置标头。
我正在使用 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与中间件有问题吗?
遗憾的是没有人更早对此做出回应。
您的代码看起来就像文档中一样,但是您将标头添加到req
代替res
.
The second problem is that the docs mislead you into(fixed) https://github.com/gruntjs/grunt-contrib-connect/issues/114#event-145223615 adding your middleware with .push
. Your code is not called at all, because something before it is doing a res.end
and/or not calling 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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)