【问题标题】:Rewrite URL with grunt用 grunt 重写 URL
【发布时间】:2015-03-15 12:20:27
【问题描述】:

我有一个需要更改一些网址的应用程序。例如localhost:9000 应该看起来像localhost:9000/myapp

所有的静态文件都会有 html 中的 url,例如 myapp/style/main.css。但实际上myapp 文件夹将不存在,我只需要在 url 中显示它。我不知道如何在内部重写它。

服务器是"grunt-contrib-connect"

【问题讨论】:

    标签: javascript url-rewriting gruntjs grunt-contrib-connect


    【解决方案1】:

    使用http-rewrite-middleware

    安装后,将其放在 Gruntfile 的顶部

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

    然后在你的连接 livereload 下做这样的事情:

    livereload: {
        options: {
          open: 'http://localhost:9000/myapp',
          middleware: function(connect, options, middlewares) {
    
            // rewrite (make sure it is first)
            middlewares.unshift(rewriteModule.getMiddleware([
              {from: '^/myapp/(.*)', to: '/$1'}
            ]));
    
            //paths
            middlewares.push(connect.static('.tmp'));
            middlewares.push(connect().use(
              '/bower_components',
              connect.static('./bower_components')
            ));
            middlewares.push(connect.static(appConfig.app));
    
            return middlewares;
          },
    

    【讨论】:

    • 好的,效果很好。现在下一个问题是静态文件没有加载,因为它们正在使用 /myapp/ 在那里加载 url 并且他们得到了一个不错的 404。
    猜你喜欢
    • 2013-08-17
    • 2014-07-27
    • 1970-01-01
    • 2012-03-07
    • 1970-01-01
    • 2016-11-15
    • 1970-01-01
    • 2010-11-04
    • 1970-01-01
    相关资源
    最近更新 更多