grunt-contrib-connect middleware CORS solution with keepalive true

后端 未结 1 1156
说谎
说谎 2021-02-13 10:59

For my local development system I am trying to serve front-end assets using grunt-contrib-connect. I need a cross-domain solution for using fonts in Firefox. The server runs jus

相关标签:
1条回答
  • 2021-02-13 11:21

    It's sad that nobody responded to that earlier.

    Your code looks just like in the documentation, but you add the headers to req instead of res.

    The second problem is that the docs mislead you into(fixed) 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().

    Your fixed code would look like this:

        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;
                    }
    
    0 讨论(0)
提交回复
热议问题