最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I handle requests to routes that don't exist - Stack Overflow

programmeradmin2浏览0评论

I'm sure information on this already exists, but I searched and don't know if my search terms are correct.

I have a node/express application and I want it so that when a user requests a route that doesn't exist, the server will return a specific page rather than the default "Cannot get X" page.

for example, if someone reqeusts

application/asdf 

but no route exists, I'd like to render the 404 page.

How does one do this?

I'm sure information on this already exists, but I searched and don't know if my search terms are correct.

I have a node/express application and I want it so that when a user requests a route that doesn't exist, the server will return a specific page rather than the default "Cannot get X" page.

for example, if someone reqeusts

application/asdf 

but no route exists, I'd like to render the 404 page.

How does one do this?

Share Improve this question asked Mar 1, 2014 at 19:04 EogcloudEogcloud 1,3654 gold badges19 silver badges46 bronze badges 3
  • 1 Found this one stackoverflow./questions/6528876/…. Helpful? – loveNoHate Commented Mar 1, 2014 at 19:22
  • 1 Found the answer in the 2nd ment, thank you! – Eogcloud Commented Mar 1, 2014 at 19:31
  • @Eogcloud They both work, but I'd suggest using the method in the first method rather than the second. – loganfsmyth Commented Mar 2, 2014 at 7:51
Add a ment  | 

1 Answer 1

Reset to default 10

Found my answer! Thank you @dollarVar

2nd answer: How to redirect 404 errors to a page in ExpressJS?

I think you should first define all your routes and as the last route add

//The 404 Route (ALWAYS Keep this as the last route)
app.get('*', function(req, res){
  res.send('what???', 404);
});
发布评论

评论列表(0)

  1. 暂无评论