0

I have been working on avoiding # in Angular app with ExpressJS serverside. I have researched how to enable html5mode and it worked great. 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.

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. 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.

1 Answer 1

1

In expressjs the routing rules are matched in the order you present them in the code. 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! I changed order of the routing syntax in the main app.js as you pointed. I was struggling with this problem for two days and finally got this. I really appreciate you for saving my time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.