简体   繁体   English

Express.js中的错误处理

[英]Error Handling in Express.js

How to handle errors in Express.js? 如何处理Express.js中的错误?

For example, if user calls non-existing resource ? 例如,如果用户调用不存在的资源

If you want to yield a 404 just add this as your last route: 如果您想要产生404只需将其添加为最后一条路线:

app.get('*', function(req, res){
  res.send('Not Found', 404);
});

So everything that wasn't handled by any other route will results in a 404. 因此,任何其他路线未处理的内容都将产生404。

You can find additional information on sending error responses in the section of the expressJS API Reference that covers the send method of the Response object. 您可以在expressJS API参考部分中找到有关发送错误响应的其他信息,该部分涵盖了Response对象的send方法。 It is located here . 它位于这里

Omen, if you want to cover all HTTP methods use: 预兆,如果要覆盖所有HTTP方法,请使用:

app.all('*', function(req, res) {

   // Do something...

});

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

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