Create an Express route to handle DELETE requests for a specific article. Which snippet is correct?
4 phương ánCó giải thích
Các phương án trả lời
Chọn một phương ánLời giảiChọn phương án để mở
Đáp án đúng: "articleRouter.route('/:id').delete(articleController.delete)". Xoá một bài viết cụ thể cần hai điều: method HTTP DELETE theo quy ước REST, và tham số đường dẫn :id để biết bài nào; router.route('/:id') gom các handler của cùng một đường dẫn, .delete(...) gắn handler cho method DELETE, còn trong controller lấy id qua req.params.id. "articleRouter.route('/').delete(articleController.delete)" thiếu :id nên đường dẫn trỏ tới cả tập bài viết, không xác định được bài cần xoá. "articleRouter.route('/:id').get(articleController.delete)" gắn handler xoá vào GET, trái ngữ nghĩa REST vì GET phải an toàn, chỉ đọc. "articleRouter.route('/:id').post(articleController.delete)" dùng POST vốn dành cho tạo mới, nên yêu cầu DELETE tới /:id sẽ không khớp route này.