简体   繁体   English

用grunt重写URL

[英]Rewrite URL with grunt

I've a app for which I need to change some url. 我有一个应用程序,我需要更改一些网址。 For example localhost:9000 should look like localhost:9000/myapp 例如localhost:9000应该看起来像localhost:9000/myapp

All the static files will have url in html like myapp/style/main.css . 所有静态文件都将在html中包含url,如myapp/style/main.css But in actual myapp folder will not exist, I just need to show it in url. 但实际上myapp文件夹不存在,我只需要在url中显示它。 I don't know how to internally rewrite it. 我不知道如何在内部重写它。

Server is "grunt-contrib-connect" 服务器是"grunt-contrib-connect"

use http-rewrite-middleware 使用http-rewrite-middleware

After you install it, put this at the top of your Gruntfile 安装后,将它放在Gruntfile的顶部

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

Then under your connect livereload do something like this: 然后在你的connect 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;
      },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM