简体   繁体   English

咕unt声:带有句点的位置网址应用

[英]Grunt: Location URL app with period

In one webapp that I'm working with, I have deal with URLs like the one below: 在一个我正在使用的Webapp中,我处理了以下URL:

http://localhost:8080/section/value.with.periods

This value.with.periods is a URL param, like the ones that you declare on angular's routeProvider : 这个value.with.periods是一个URL参数,就像您在angular的routeProvider上声明的routeProvider

angular.config(['$routeProvider', function ($routeProvider) {
    $routeProvider
        .when('/section/:param', {
            templateUrl: 'url-to-template',
            controller: 'ExampleCtrl',
            resolve: {
                ...
            }
        });
}]);

The problem is that the server used, running under Grunt tasks, cannot handle URLs with periods in it: 问题是所使用的服务器在Grunt任务下运行,无法处理其中带有句点的URL:

Cannot GET /section/value.with.periods

I'm running Grunt with grunt-contrib-proxy and connect-modrewrite , and the livereload task, which configures the connect-modrewrite , is the one below: 我正在使用grunt-contrib-proxyconnect-modrewrite运行Grunt,并且配置了connect-modrewritelivereload任务如下:

        livereload: {
            options: {
                open: 'http://localhost:<%= connect.options.port %>',
                base: [
                    '.tmp',
                    '<%= config.app %>'
                ],
                middleware: function(connect, options) {
                    if (!Array.isArray(options.base)) {
                        options.base = [options.base];
                    }

                    // Setup the proxy
                    var middlewares = [proxySnippet];

                    var modRewrite = require('connect-modrewrite');
                    middlewares.push(modRewrite(['^[^\\.]*$ /index.html [L]']));
                    // middlewares.push(modRewrite(['!\\.html|\\.js|\\.svg|\\.css|\\.png|\\.jpg\\.gif|\\swf$ /index.html [L]']));


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

I need to be capable to deal with URLs with periods on the params used on Angular. 我需要能够处理在Angular上使用的带有句点的URL。 Any help will be appreciated. 任何帮助将不胜感激。

Thanks. 谢谢。

Your rewrite regex excludes all paths with a period in it: 您的重写正则表达式会排除其中带有句点的所有路径:

^[^\\.]*$

That means as much as: match urls with all characters, except when they have a backslash or a period. 这意味着:与所有字符匹配url,除非它们带有反斜杠或点号。 So /section/value.with.periods would be ignored. 因此/section/value.with.periods将被忽略。

You should change your regex to something more forgiving: 您应该将正则表达式更改为更宽容的内容:

middlewares.push(modRewrite(['^(.*)$ /index.html [L]']));

And you should be good to go. 而且您应该很好。

Edit solution: 编辑解决方案:

In the comments we came to the answer: above regex will rewrite all urls to index.html, causing other files not to be served. 在评论中,我们得到了答案:上面的regex将所有URL重写为index.html,从而导致其他文件无法提供。 There was a commented out line that only rewrites urls with unknown file extensions: 有一条注释行,仅重写具有未知文件扩展名的URL:

middlewares.push(modRewrite(['!\\.html|\\.js|\\.svg|\\.css|\\.png|\\.jpg|\\.gif|\\.swf$ /index.html [L]']));

That gives the desired result. 那给出了期望的结果。 See comments for more info. 查看评论以获取更多信息。

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

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