Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js / Expess.js. Static router not recognized if used with wildcard router

When I use only this route with my all other routes, that static files served well:

var public_dir = path.join(__dirname, 'public');
app.use('/public', express.static(public_dir));

But when I add below these lines - all requests are catch with this router (not the previous one):

app.get('/*', function(req, res){
    res.redirect('/#!' + req.path);
});

It's strange to me, because /public definition comes first. But all request goes to last defined /* request handler.

Now, if I try to open page http://127.0.0.1:3000/public/website/application.js I become redirected to http://127.0.0.1:3000/#!/public/website/application.js. Why it happens, and how to solve this situation in the best manner?

Thank you a lot!


Solution is simple. I have not working statement in list of middlewares.

app.use(app.router);

This is creates a problem of url routing. But it's strange that node.js did not tell me about "incorrect setup" of middleware.

like image 862
Anton Danilchenko Avatar asked Oct 02 '22 03:10

Anton Danilchenko


1 Answers

There is nothing wrong with your code but keep in mind that any request on something that doesn't exist will be catch by your get('/*', ...).

This said, you load public_dir on /public that's mean if public_dir is defined as public/website/ you have to do http://127.0.0.1:3000/public/application.js in order to access a file locate at /public/website/application.js

like image 199
BaNz Avatar answered Oct 13 '22 09:10

BaNz