angularjs - Grunt 连接代理 : 404 Not Found

标签 angularjs proxy gruntjs grunt-connect-proxy

我正在使用grunt-connect-proxy “^0.2.0”从我的 angularjs 应用程序代理到 api。该项目是从 yeoman Angular 生成器开始的。

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

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 正在运行。

最佳答案

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

这正是您想要做的。

关于angularjs - Grunt 连接代理 : 404 Not Found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35762017/

相关文章:

json - Golang 将 JSON 对象传递给函数

http - 没有 SSL 集成的 Web 应用程序被代理拦截

javascript - 如何使用 Grunt 将 mains 文件复制到特定文件夹

node.js - Grunt 缩小了 JS 和 CSS 文件,其版本位于浏览器缓存中

node.js - GRUNT 安装时不带 package.json

javascript - AngularJS jsonp 成功错误

jquery - jQuery 自动完成 + AngularJS 的问题

proxy - capistrano 通过代理部署到没有公共(public) IP 的服务器

angularjs - Angular js 当达到最大字符长度时将焦点移动到下一个输入字段,当字符长度最小时将焦点移动到上一个输入字段

javascript - AngularJS 和服务内部的异步函数