【问题标题】:grunt-contrib-connect middleware CORS solution with keepalive true具有 keepalive true 的 grunt-contrib-connect 中间件 CORS 解决方案
【发布时间】:2014-05-24 10:45:56
【问题描述】:

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

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

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 与中间件一起使用有问题吗?

【问题讨论】:

    标签: cors grunt-contrib-connect


    【解决方案1】:

    很遗憾,之前没有人回应。

    您的代码与文档中的代码类似,但您将标头添加到 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;
                    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    相关资源
    最近更新 更多