概览
在使用 Express 开发 Web 应用程序时,有效地处理过错和办理未匹配任何界说处理程序的路由至关重要。这确保了应用程序的健壮性和更好的用户体会。
Express 中的过错处理
Express 经过中间件提供了内置的过错处理机制。在 Express 应用程序中进行适当的过错处理包含界说过错处理中间件函数,以捕获过错并作出相应呼应。
分步指南
1. 创建过错处理中间件
过错处理中间件界说了四个参数:err
、req
、res
和 next
。
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('出错了!');
});
2. 使用 next()
next()
函数至关重要。它将过错传递给链中的下一个过错处理中间件。在路由处理程序或其他中间件中,调用 next(err)
将过错传递给过错处理程序。
app.get('/route', (req, res, next) => {
try {
// ... 您的逻辑
} catch (err) {
next(err);
}
});
3. 处理异步过错
关于异步代码,使用 catch
或与 promises 和 async/await 一同使用 next
来处理过错。
app.get('/async-route', async (req, res, next) => {
try {
// 测验运转异步操作
const data = await someAsyncOperation();
// 假如成功,将数据发送回客户端
res.send(data);
} catch (err) {
// 假如 someAsyncOperation() 中产生过错,捕获它
// 然后将过错传递给下一个过错处理中间件
next(err);
}
});
处理未匹配的路由
未处理的路由是指不匹配任何界说路由的恳求。高雅地处理这些路由是一个良好的实践。
处理未处理路由的步骤
1. 界说全捕获路由
在所有路由之后,添加一个全捕获路由处理程序。
app.use('*', (req, res) => {
res.status(404).send('404 - 未找到');
});
这将捕获对未界说路由的任何恳求,并回来 404 状态码。
2. 自界说呼应
您能够根据需要自界说 404 呼应,比方烘托一个 404 页面或回来一个 JSON 呼应。
app.use('*', (req, res) => {
res.status(404).render('NotFound');
});
结论
在 Express 中适当地处理过错和办理未匹配的路由是安稳和用户友好 Web 应用程序的要害组成部分。经过实施这些策略,您能够确保您的应用程序行为可猜测,并在产生过错或拜访过错路由时为用户提供信息性反馈。
English version: Handling Errors and Unhandled Routes in Express
Overview
When developing web applications with Express, it’s crucial to handle errors effectively and manage routes that do not match any defined handlers. This ensures a robust application and a better user experience.
Error Handling in Express
Express provides a built-in mechanism for error handling through middleware. Proper error handling in an Express application involves defining error-handling middleware functions that catch errors and respond accordingly.
Step-by-Step Guide
1. Creating an Error Handling Middleware
An error handling middleware is defined with four arguments: err
, req
, res
, and next
.
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something broke!');
});
2. Using next()
The next()
function is crucial. It passes the error to the next error handling middleware in line. In your route handlers or other middleware, call next(err)
to pass errors to your error handler.
app.get('/route', (req, res, next) => {
try {
// ... your logic
} catch (err) {
next(err);
}
});
3. Handling Asynchronous Errors
For asynchronous code, use catch
or next
with promises and async/await to handle errors.
app.get('/async-route', async (req, res, next) => {
try {
// Attempt to run the asynchronous operation
const data = await someAsyncOperation();
// If successful, send the data back to the client
res.send(data);
} catch (err) {
// If an error occurs in someAsyncOperation(), catch it
// Then pass the error to the next error handling middleware
next(err);
}
});
Handling Unmatched Routes
Unhandled routes are requests that do not match any of the defined routes. It’s good practice to handle these gracefully.
Steps for Handling Unhandled Routes
1. Define a Catch-All Route
After all your routes, add a catch-all route handler.
app.use('*', (req, res) => {
res.status(404).send('404 - Not Found');
});
This will catch any requests to undefined routes and return a 404 status code.
2. Customize the Response
You can customize the 404 response as needed, maybe rendering a 404 page or returning a JSON response.
app.use('*', (req, res) => {
res.status(404).render('NotFound');
});
Conclusion
Proper error handling and managing unmatched routes in Express are key components of a stable and user-friendly web application. By implementing these strategies, you can ensure that your application behaves predictably and provides informative feedback to the user in case of errors or incorrect route access.