8

我正在使用grunt-connect-proxy "^0.2.0" 从我的 angularjs 应用程序代理到 api。该项目是从 yeoman angular-generator 开始的。

我已按照此处的说明进行操作,但是使用代理时,我得到:

Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:9000/api/users 

我的代理配置:

 connect: {
  options: {
    port: 9000,
    // Change this to '0.0.0.0' to access the server from outside.
    hostname: 'localhost',
    livereload: 35729
  },
  proxies: [{
    context: '/api', // the context of the data service
    host: 'localhost', // wherever the data service is running
    port: 8080, // the port that the data service is running on
    https: false,
    xforward: false
  }],

我的中间件:

livereload: {
    options: {
      open: true,
      base: '',
      middleware: function (connect, options) {
        var middlewares = [];

        middlewares.push(require('grunt-connect-proxy/lib/utils').proxyRequest);

        middlewares.push(connect.static('.tmp'));
        middlewares.push(connect.static('test'));
        middlewares.push(connect().use(
            '/bower_components',
            connect.static('./bower_components')
          ));
        middlewares.push(connect.static(appConfig.app));

        return middlewares;
      }
    }
  },

我的服务任务

grunt.registerTask('serve', 'Compile then start a connect web server', function (target) {
  if (target === 'dist') {
    return grunt.task.run(['build', 'connect:dist:keepalive']);
  }

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

编辑 localhost:8080/users 当前通过 Postman 返回 403,因此 API 正在运行。

4

2 回答 2

0

不直接回答您的问题,而是提供解决方案(因为没有其他人回答过)...您是否尝试过 npm 模块 connect-modrewrite

这正是你想要做的。

于 2016-03-18T09:46:18.463 回答
0

我遇到了同样的问题,我用这种方式解决了它,它对我有用。在下面查看我的代码:-

connect: { 
            server: { 
                options: { 
                    keepalive: true, 
                    port: 8001, 
                    protocol: 'http', 
                    hostname: '*', 
                    directory: 'dist', 
                    open: { 
                        target: 'http://localhost:8001/myDemo.html', 

                    },
                        middleware: function(connect, options, middlewares) {

                                middlewares.unshift(function(req, res, next) {
                                    res.setHeader('Access-Control-Allow-Credentials', true);
                                    res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
                                    res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
                                    **if (req.method.toUpperCase() == 'POST') req.method='GET';**
                                    return next();
                                });

                                return middlewares;
                        }

                } 
            } 
        },

看到星标线即 if (req.method.toUpperCase() == 'POST') req.method='GET'; 我做了这个把戏,它对我有用。这篇文章对我也有帮助https://github.com/gruntjs/grunt-contrib-connect#middleware

于 2016-12-05T10:14:29.333 回答