如何设置grunt服务器的基本URI

问题描述 投票:0回答:1

我使用了 yeoman webapp 生成器来构建我的应用程序。

在开发过程中,我只是使用

grunt serve
来预览应用程序。

现在我已准备好部署我的应用程序,我发现生产环境具有与开发环境不同的根上下文。换句话说,而不是:

http://base:port/

我被迫使用

http://base:port/subdir_name/

当然,这会破坏一些东西。

有什么方法可以配置 grunt 使用的

connect
服务器,以将此新的根上下文作为它的基本 URI?

node.js gruntjs yeoman node.js-connect
1个回答
2
投票

我不确定您是否已经找到解决方案,但我设法通过使用 grunt-connect-proxy 重写其重定向功能来解决此问题。这就是我的配置:

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;
      }
    }
  },
© www.soinside.com 2019 - 2024. All rights reserved.