如何处理 404 响应?
在 Express 中,404 响应不是错误的结果,因此错误处理程序中间件不会捕获它们。这种行为是因为 404 响应只是表明没有额外的工作要做;也就是说,Express 已经执行了所有的中间件函数和路由,发现没有一个响应。您需要做的就是在堆栈的最底部(在所有其他函数下方)添加一个中间件函数来处理 404 响应:
In Express, 404 responses are not the result of an error, so the error-handler middleware will not capture them. This behavior is because a 404 response simply indicates the absence of additional work to do; in other words, Express has executed all middleware functions and routes, and found that none of them responded. All you need to do is add a middleware function at the very bottom of the stack (below all other functions) to handle a 404 response:
app.use((req, res, next) => {
res.status(404).send("Sorry can't find that!")
})
af584/owJAROjVX4bQyD/EajFds3/TsgcM8+rJ4sXHL7xAyNLTc9hhoI1af4eI6XMOv1YSXqiea4NQfWsvS+Oc7/nNs+0MOLhrYg6yLpYGYEgtwYbRO1BqJXQC5BpiC0v+7jcrZAtb8wfBvihC0HZClkb68cpeHxE8LLXMK8sIAEMo73Nc5V5BfYoiOdopXXMa9kP9jhmsZSYeEQY+S1xD5FEl+gpPItecgnBta3j9fosXAZp9nHeoR0qhK2Y1v3Qyi0YMxOg5CYc6maaklaQQJdC9GYxVK2ryuzHwD4ai4TNgaYOfF8VbvU/lSMU8Z2z16SN5tsW6vrmHGQ42X0QBQEQkgcDsibCTybN/zmgqtNuzQGR81mcE7RWV36G9OG0nxeTU9FmRrMNNKpMxNgKg==