简体   繁体   English

Express.js和Angular.js html5mode(在网址中避免#)

[英]Express.js and Angular.js html5mode (Avoiding # in url)

I have been working on avoiding # in Angular app with ExpressJS serverside. 我一直在努力避免在ExpressJS服务器端的Angular应用中使用#。 I have researched how to enable html5mode and it worked great. 我研究了如何启用html5mode,效果很好。 But whenever there is another 'get' request to retrieve data from another url such as /api/services, it seems like somehow broken and do not provide data properly to the page. 但是,每当有另一个“获取”请求从另一个url(例如/ api / services)检索数据时,它似乎以某种方式损坏了并且不能正确地向页面提供数据。

Here's what I have done in express end. 这是我在结束时所做的。

router.get('/*', function (req, res, next) {
  res.render('index');
});
router.get('/api/service-edit', function (req, res, next) {
  Service.find(function (err, services) {
    if (err) {return next(err);}
    res.json(services);
  });
});

I am not 100% sure, but what I guess is '/*' causes a problem for the server in reading api urls. 我不确定100%,但是我猜是“ / *”会导致服务器在读取api网址时出现问题。 I am wondering if any of you have an experience in this problem and a solution to figure this out. 我想知道你们中是否有人在这个问题上有经验,以及解决这个问题的解决方案。

Thank you for your time. 感谢您的时间。

In expressjs the routing rules are matched in the order you present them in the code. 在expressjs中,路由规则按照您在代码中显示的顺序进行匹配。 So the '/*' rule is always matched first and the second rule is never reached. 因此,始终首先匹配“ / *”规则,而永远不会达到第二个规则。

Putting the second rule before the first one will probably solve the problem. 将第二个规则放在第一个规则之前可能会解决此问题。

Hope it helps. 希望能帮助到你。

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

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