點燈坊

失くすものさえない今が強くなるチャンスよ

使用 DELETE 刪除資料

Sam Xiao's Avatar 2021-10-26

若要刪除資料,可使用 DELETE 透過 Params 傳遞 ID。

Version

Express 4.17.1

app.delete

import express from 'express'
import cors from 'cors'

let app = express ()
app.use (cors ())
app.use (express.json ())

app.delete ('/api/articles/:id', (req, res) => res.json ({
  id: req.params.id,
  title: 'Title 1',
  content: 'Content 1'
}))
app.listen (8080, _ => console.log ('Node listen on port: 8080'))

第 8 行

app.delete ('/api/articles/:id', (req, res) => res.json ({
  id: req.params.id,
  title: 'Title 1',
  content: 'Content 1'
}))
  • 使用 DELETE 刪除資料時,id 在 URL 以 params 形式傳入
  • 使用 app.delete 實現 DELETE

delete000

Conclusion

  • app.delete 實現 DELETE
  • req.params 讀取 id

Reference

Express, app.delete