0

我有一个 yeoman angular 应用程序,我的配置grunt-connect-proxy如下Gruntfile.js

module.exports = function (grunt) {
  grunt.loadNpmTasks('grunt-connect-proxy');

[...]

connect: {
  options: {
    port: 9000,
    // Change this to '0.0.0.0' to access the server from outside.
    hostname: 'localhost',
    livereload: 35729
  },
  dev: {
    proxies: [
    {
      context: '/api/v1',
      host: '127.0.0.1',
      port: 3000,
      https: false,
    }
    ]
  },
  pro: {
    appendProxies: false,
    proxies: [{
      context: '/api',
      host: 'example.org'
    }]
  },
  livereload: {
    options: {
      open: true,
      middleware: function (connect) {
        return [
          require('grunt-connect-proxy/lib/utils').proxyRequest,
          connect.static('.tmp'),
          connect().use(
            '/bower_components',
            connect.static('./bower_components')
          ),
          connect().use(
            '/app/styles',
            connect.static('./app/styles')
          ),
          connect.static(appConfig.app)
        ];
      }
    }
  },
  test: {
    options: {
      port: 9001,
      middleware: function (connect) {
        return [
          connect.static('.tmp'),
          connect.static('test'),
          connect().use(
            '/bower_components',
            connect.static('./bower_components')
          ),
          connect.static(appConfig.app)
        ];
      }
    }
  },
  dist: {
    options: {
      open: true,
      base: '<%= yeoman.dist %>'
    }
  }
},

[...]

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',
    'postcss:server',
    'configureProxies:dev',
    'connect:livereload',
    'watch'
  ]);
});


grunt.registerTask('build', [
  'clean:dist',
  'configureProxies:pro',
  'wiredep',
  'useminPrepare',
  'concurrent:dist',
  'postcss',
  'ngtemplates',
  'concat',
  'ngAnnotate',
  'copy:dist',
  'cdnify',
  'cssmin',
  'uglify',
  'filerev',
  'usemin',
  'htmlmin'
]);

我在角度控制台中得到了这个:

Running "configureProxies:dev" (configureProxies) task
Proxy created for: /api/v1 to 127.0.0.1:3000

我的资源:

'use strict';

/**
 * @ngdoc service
 * @name doMasApp.Client
 * @description
 * # Client
 * Service in the doMasApp.
 */
angular.module('doMasApp')
.factory('Client', [
    '$resource',
    function ($resource) {
      var client = $resource(
          '/clients/:id',
          { id: '@id' },
          {
            query: { isArray: false },
            update: { method: 'PUT' }
          });
      return client;
    }])
;

但是当我尝试连接到服务器并获取资源时,我得到了这个:

GET http://localhost:9000/clients 404 (Not Found)

请帮忙!!!

4

1 回答 1

0

工厂网址必须是这样的:

angular.module('doMasApp')
.factory('Client', [
    '$resource',
    function ($resource) {
      var client = $resource(
          '/aoi/v1/clients/:id',
          { id: '@id' },
          {
            query: { isArray: false },
            update: { method: 'PUT' }
          });
      return client;
    }])
;

代理上下文必须添加到工厂 url。

于 2016-06-19T18:34:05.340 回答