Handling Errors in Express
メニューを表示するにはスワイプしてください
Errors can happen when something goes wrong, such as invalid input or missing data.
In Express, you can send error responses using the response object.
app.get('/users/:id', (req, res) => {
const id = Number(req.params.id);
const user = users.find(u => u.id === id);
if (!user) {
return res.status(404).send('user not found');
}
res.json(user);
});
Here, if the user is not found, the server sends a response with a status code and a message.
Status codes help indicate what happened:
- 200: success;
- 404: resource not found;
- 500: server error.
Handling errors properly makes your API more predictable and easier to use.
すべて明確でしたか?
フィードバックありがとうございます!
セクション 1. 章 15
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください
セクション 1. 章 15