删除特定id的商品
show.ejs <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> <%= product.name%> </title> </head> <body> <h1> <%= product.name%> </h1> <ul> <li>Price: $<%= product.price%> </li> <li>Category: <%= product.category%> </li> </ul> <a href="/products">All products</a> <a href="/products/<%=product._id%>/edit">edit</a> <form action="/products/<%=product._id%>?_method=DELETE" method="POST"> <button> Delete </button> </form> </body> </html>
app.delete('/products/:id', async (req, res) => { const { id } = req.params; const product = await Product.findByIdAndDelete(id); res.redirect('/products'); //删除之后返回所有商品的列表 })