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

bookUpdating and Deleting Data

メニューを表示するにはスワイプしてください

In addition to creating data, APIs often need to update and delete existing records.

To update data, you can use PUT or PATCH requests.

app.put('/users/:id', (req, res) => {
  const id = Number(req.params.id);
  const updatedData = req.body;

  const user = users.find(u => u.id === id);

  if (user) {
    user.name = updatedData.name;
    res.json(user);
  } else {
    res.send('User not found');
  }
});

To delete data, you use a DELETE request:

app.delete('/users/:id', (req, res) => {
  const id = Number(req.params.id);

  users = users.filter(u => u.id !== id);

  res.send('User deleted');
});

Examples:

  • PUT '/users/1': updates user with id 1;
  • DELETE '/users/1': deletes user with id 1.

These operations allow you to modify and remove data in your application.

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  14

AIに質問する

expand

AIに質問する

ChatGPT

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

セクション 1.  14
some-alt