How to add CORS headers to a Meteor app?

后端 未结 2 781
Happy的楠姐
Happy的楠姐 2020-12-09 04:58

How it is possible to add Access-Control-Allow-Origin: * header to all responses (in particular, I am interested for static files under /public/) i

2条回答
  •  醉酒成梦
    2020-12-09 05:42

    Here is a little snippet I wrote. You can use as an example in how to access meteor's core connect and modify headers, also a pretty good drop-in for every meteor project:

    /**
     * HTTP Header Security
     *
     * enforce HTTP Strict Transport Security (HSTS) to prevent ManInTheMiddle-attacks
     * on supported browsers (all but IE)
     * > http://www.html5rocks.com/en/tutorials/security/transport-layer-security
     *
     * @header Strict-Transport-Security: max-age=2592000; includeSubDomains
     */
    
    var connectHandler = WebApp.connectHandlers; // get meteor-core's connect-implementation
    
    // attach connect-style middleware for response header injection
    Meteor.startup(function () {
      connectHandler.use(function (req, res, next) {
        res.setHeader('Strict-Transport-Security', 'max-age=2592000; includeSubDomains'); // 2592000s / 30 days
        return next();
      })
    })
    

提交回复
热议问题