Routing with express.js - Cannot GET Error

柔情痞子 提交于 2019-12-01 06:42:19
bryanmac

Try changing the routes from most specific to least specific. Routes are matched in order. If it matches on the '/' route first, it will pass to routes.index, and never get a chance to router to the /user/:id/photos function.

So, instead of:

app.get('/', routes.index);
....
app.get('/user/:id/photos', function(req,res){

Try:

app.get('/user/:id/photos', function(req,res){
....
app.get('/', routes.index);

As a side note, I think /user/photos/:id seems better but I tried out /something/:id/somethingelse in my app, and it worked fine.

Your example looks fine. It works for me (express 2.5.8, node 0.6.11). Actually, while bryanmac is correct that order matters, a request for /user/1/photos won't match the route / - it would for other routes, e.g. /*, but not / (or /test for that matter).

What version of express, node do you run? Also, can you post your routes.js too?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!