How can I add CORS-Headers to a static connect server?

前端 未结 4 821
庸人自扰
庸人自扰 2020-12-03 17:15

I am writing a little demo web server delivering static html,css and javascript. The server looks like

(function () {
    \"use strict\";

    var http = req         


        
4条回答
  •  抹茶落季
    2020-12-03 17:24

    I hope this will help:

    //CORS middleware
    var allowCrossDomain = function(req, res, next) {
        res.header('Access-Control-Allow-Origin', config.allowedDomains);
        res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
        res.header('Access-Control-Allow-Headers', 'Content-Type');
    
        next();
    }
    
    //...
    app.configure(function() {
    
        app.use(allowCrossDomain);
        app.use(express.static(__dirname + '/public'));
    });
    

    More detail: How to allow CORS?

提交回复
热议问题