首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Grunt:带有句点的位置URL应用程序

Grunt:带有句点的位置URL应用程序
EN

Stack Overflow用户
提问于 2015-01-20 21:21:50
回答 1查看 383关注 0票数 1

在我正在使用的一个webapp应用程序中,我处理的URL如下所示:

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

这个value.with.periods是一个URL,就像您在角的routeProvider上声明的那样

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

问题是,在Grunt任务下运行的服务器无法处理其中包含句点的URL:

Cannot GET /section/value.with.periods

我使用grunt-contrib-proxyconnect-modrewrite运行Grunt,而配置connect-modrewritelivereload任务如下所示:

代码语言:javascript
复制
        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;
                }
            }
        }

我需要有能力处理URL与句点上使用的参数角。任何帮助都将不胜感激。

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-20 22:43:54

重写regex排除包含句点的所有路径:

代码语言:javascript
复制
^[^\\.]*$

这意味着:将urls与所有字符匹配,除非它们有反斜杠或句点。所以/section/value.with.periods会被忽略。

你应该把你的准则改为更宽容的东西:

代码语言:javascript
复制
middlewares.push(modRewrite(['^(.*)$ /index.html [L]']));

你应该可以走了。

编辑解决方案:

在注释中,我们找到了答案:上面的regex将将所有urls重写到index.html,导致其他文件无法得到服务。有一个注释行,它只重写具有未知文件扩展名的urls:

代码语言:javascript
复制
middlewares.push(modRewrite(['!\\.html|\\.js|\\.svg|\\.css|\\.png|\\.jpg|\\.gif|\\.swf$ /index.html [L]']));

这就给出了预期的结果。有关更多信息,请参见评论。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28055374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档