简体   繁体   English

从Express路由中排除带有文件扩展名的路由

[英]Exclude routes with file extension from Express routes

I'd like AngularJs to deal with most of my routes so I don't need to specify routes in Express AND Angular at the same time. 我希望AngularJs处理我的大部分路线,因此不需要同时在Express AND Angular中指定路线。 So I created a "catch all" route like this: 因此,我创建了一条“全包”路线,如下所示:

app.use('/api', api); // server-side stuff
app.use(express.static(__dirname + '/public')); //static files
app.get('*', function (req, res) { //angular stuff
    res.render('index');
});

Then on my Angular app, if the route doesn't exist, I handle it this way 然后在我的Angular应用中,如果路由不存在,我将以这种方式处理

$routeProvider.otherwise({
    templateUrl: '/partials/404.html', 
    controller: 'DummyCtrl'
});

The problem is that, if the static() route does not match a file in the public directory, it will then go to the next "catch all" route. 问题是,如果static()路由与公用目录中的文件不匹配,则它将转到下一个“全部捕获”路由。

This cause some issues as the server returns res.render('index'); 当服务器返回res.render('index');这会导致一些问题res.render('index'); (the whole page) which then is injected in the page by AngularJs so I get a warning WARNING: Tried to load angular more than once. (整个页面),然后由AngularJs注入到页面中,因此我得到警告WARNING: Tried to load angular more than once. .

I'd like to re-write my "catch all" route to only match routes if they don't match files. 我想将我的“全部捕获”路由重写为仅匹配不匹配文件的路由。

I believe what I need is a regex for the Angular route that would exclude routes if they look like files. 我相信我需要的是Angular路由的正则表达式,如果它们看起来像文件,它将排除路由。 Eg: exclude " .html," " .png", etc. 例如:排除“ .html”,“ .png”等。

解决方案如下:/ /\\/[^.]*$|^(?:https?:)?\\/\\/[^\\/]+$/ :)? /\\/[^.]*$|^(?:https?:)?\\/\\/[^\\/]+$/

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

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