node.jsgruntjsyeomannode.js-connect

How to set the base URI for the grunt server


I've used a yeoman webapp generator to build my app.

During the development, I simply used grunt serve to preview the app.

Now that I'm ready to deploy my app, I've found out that the production environment has a different root context than the development one. In other words, instead of:

http://base:port/

I'm forced to use

http://base:port/subdir_name/

This, of course, breaks some stuff.

Is there any way to configure the connect server that grunt uses, to take this new root context as it's base URI?


Solution

  • I'm not sure if you've found a solution already, but I managed to work around this by using grunt-connect-proxy capability of rewriting it's redirects. This is what my config looks like:

    connect: {
      options: {
        port: 8001,
        // Change this to '0.0.0.0' to access the server from outside.
        hostname: '0.0.0.0',
        livereload: 35729
      },
      proxies: [
        {
            context: '/subdir_name/',
            host: 'localhost',
            port: 8001,
            https: false,
            xforward: false,
            rewrite: {
              '^/subdir_name': ''
            },
            headers: {
                'x-forwarded-server': 'Grunt Localhost development'
            }
        }
      ],
      livereload: {
        options: {
          open: true,
          middleware: function (connect, options) {
            if (!Array.isArray(options.base)) {
                options.base = [options.base];
            }
    
            // Setup the proxy
            var middlewares = [];
    
            middlewares.push(connect.static('.tmp'));
            middlewares.push(connect().use(
                '/bower_components',
                connect.static('./bower_components')
              ));
            middlewares.push(connect().use(
                '/app/styles',
                connect.static('./app/styles')
              ));
            middlewares.push(connect.static(appConfig.app));
            middlewares.push(require('grunt-connect-proxy/lib/utils').proxyRequest);
            // Serve static files.
            options.base.forEach(function(base) {
                middlewares.push(connect.static(base));
            });
    
            // Make directory browse-able.
            var directory = options.directory || options.base[options.base.length - 1];
            middlewares.push(connect.directory(directory));
            return middlewares;
          }
        }
      },