-1

Anyone succeded to use "http-rewrite-middleware" with "generator-cg-angular"??

I try this setup in Gruntfile.js, similar to what they recomend for Grunt with connect.

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

And then in grunt.initConfig I have

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;
          }
      }
  }
}

Then I get this output running "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

But when I do $http.get('rest/user') it will give me this output

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

But if I change to port 8000, this will work.

I have earlier used "grunt-connect-rewrite" and there it worked out of the box more or less, I never had this issue with port 8000, there it asks on the port I asked with no problems.

What extra do I need to configure to make this work in cg-angular without having to add port 8000 on the requests?

1 Answers1

0

Solution for "http-rewrite-middleware" with "generator-cg-angular" in Gruntfile.js:

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;
          }
    },
  }
}