AngularJS with Grunt - Connect to another server

扶醉桌前 提交于 2019-12-04 14:52:22

Your proxy setting is correct. However there are 3 more steps that you need.

First, you need to make sure that you have the grunt-connect-proxy in your npm dependencies and your package.json is updated.

npm install grunt-connect-proxy --save-dev

Secondly, you need to add the middleware to your livereload so the proxy will be put in the middle of the request chain.

  livereload: {
    options: {
      open: true,
      base: [
        '.tmp',
        '<%= yeoman.app %>'
      ],
      middleware: function (connect, options) {
        if (!Array.isArray(options.base)) {
          options.base = [options.base];
        }

        // Setup the proxy
        var middlewares = [require('grunt-connect-proxy/lib/utils').proxyRequest];

        // Serve static files.
        options.base.forEach(function(base) {
          middlewares.push(connect.static(base));
        });

        // Make directory browse-able.
        var directory = options.directory || options.base[options.base.length - 1];
        middlewares.push(connect.directory(directory));

        return middlewares;
      }
    }
  },

Lastly, you just need to call this task in the serve task by calling configureProxies before calling livereload.

grunt.task.run([
  'clean:server',
  'bower-install',
  'concurrent:server',
  'autoprefixer',
  'configureProxies',
  'connect:livereload',
  'watch'
]);

Try it out and let me know if it works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!