-1

任何人成功使用“http-rewrite-middleware”和“generator-cg-angular”?

我在 Gruntfile.js 中尝试了这个设置,类似于他们为 Grunt 推荐的带有连接的设置。

var rewriteModule = require('http-rewrite-middleware');

然后在 grunt.initConfig 我有

connect: {
  main: {
    options: {
      port: 9001,
      hostname: 'localhost'
    },
  },
  development: {
      options: {
          middleware: function (connect, options) {
              var middlewares = [];

              // RewriteRules support
              middlewares.push(rewriteModule.getMiddleware([
                  {from: '^/rest/user', to: '/json/user.json'}
              ]));

              if (!Array.isArray(options.base)) {
                  options.base = [options.base];
              }

              var directory = options.directory || options.base[options.base.length - 1];
              options.base.forEach(function (base) {
                  // Serve static files.
                  middlewares.push(connect.static(base));
              });

              // Make directory browse-able.
              middlewares.push(connect.directory(directory));

              return middlewares;
          }
      }
  }
}

然后我让这个输出运行“Grunt serve”

Running "connect:main" (connect) task
Started connect web server on http://localhost:9001
Running "connect:development" (connect) task
Rewrite rule created for: [REWRITE: ^/rest/user -> /json/user.json].
Started connect web server on http://localhost:8000

但是当我做 $http.get('rest/user') 它会给我这个输出

"NetworkError: 404 Not Found - http://localhost:9001/rest/user"

但是,如果我更改为端口 8000,这将起作用。

我之前使用过“grunt-connect-rewrite”,它或多或少是开箱即用的,我从来没有遇到过端口 8000 的问题,它在我询问的端口上询问没有问题。

我需要额外配置什么才能在 cg-angular 中进行这项工作,而无需在请求中添加端口 8000?

4

1 回答 1

0

Gruntfile.js 中带有“generator-cg-angular”的“http-rewrite-middleware”解决方案:

connect: {
  main: {
    options: {
      port: 9000,
      hostname: 'localhost',
      base: '',
      middleware: function (connect, options) {
              var middlewares = [];

              // RewriteRules support
              middlewares.push(rewriteModule.getMiddleware([
                  {from: '^/rest/user', to: '/json/me.json'}
              ]));

              if (!Array.isArray(options.base)) {
                  options.base = [options.base];
              }

              var directory = options.directory || options.base[options.base.length - 1];
              options.base.forEach(function (base) {
                  // Serve static files.
                  middlewares.push(connect.static(base));
              });

              // Make directory browse-able.
              middlewares.push(connect.directory(directory));

              return middlewares;
          }
    },
  }
}
于 2015-04-23T12:57:54.850 回答