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

前端 未结 4 832
庸人自扰
庸人自扰 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:27

    The easiest method, if you are using gulp would be to use gulp plugin called "gulp-connect" and "connect-modrewrite" and define a new gulptask to redirect a particular api. This is to make the apache act as a Proxy for the particular api, to bypass the pre-flight request, inorder to avoid CORs issue. I used the following gulp task to overcome this problem.

    var connect = require('gulp-connect'),
        modRewrite = require('connect-modrewrite');
    /**
    * Proxy Config
    */
    gulp.task('connect', function () {
      connect.server({
        root: ['./.tmp', './.tmp/{folderLocations}', './src', './bower_components'],
        port: 9000,
        livereload: true,
        middleware: function (connect, opt) {
          return [
            modRewrite([
              '^/loginProxy/(.*)$ http://app.xyzdomain.com/service/login/auth/$1 [P]'
            ])
          ];
        }
      });
    });
    

提交回复
热议问题