Angularjs Grunt连接代理:找不到404

Angularjs Grunt连接代理:找不到404,angularjs,proxy,gruntjs,grunt-connect-proxy,Angularjs,Proxy,Gruntjs,Grunt Connect Proxy,我正在使用“^0.2.0”代理angularjs应用程序中的api。该项目由约曼角发电机启动 我已经按照说明进行了操作,但当使用代理时,我得到: Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:9000/api/users 我的代理配置: connect: { options: { port: 9000, // Change t

我正在使用“^0.2.0”代理angularjs应用程序中的api。该项目由约曼角发电机启动

我已经按照说明进行了操作,但当使用代理时,我得到:

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;
      }
    }
  },
我的中间件:

 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/用户当前通过Postman返回403,因此API正在运行。

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


这正是您想要做的。

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

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';我做了这个把戏,它对我有效。这个aricle对我也有帮助

您在选项之前尝试过服务器:{}吗?看看这个问题。。也许会有帮助