Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Handling Errors in Express | Section
Building APIs with Express.js

bookHandling 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に質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  15
some-alt