Analyze the following Express.js code snippet and determine what response will be sent when a DELETE request is made to the /users endpoint.

4 phương ánCó giải thích
Đề bài
const express = require('express');
const app = express();

app.all('/users', (req, res, next) => {
  console.log('Accessing the /users endpoint...');
  next(); // pass control to the next handler
});

app.delete('/users', (req, res) => {
  res.send('DELETE request to the /users endpoint');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Các phương án trả lời

Chọn một phương án
Lời giảiChọn phương án để mở
Đáp án đúng: "The response will be 'DELETE request to the /users endpoint'.". Lần theo mã: app.all('/users') khớp mọi phương thức HTTP tới /users nên chạy trước, in dòng Accessing the /users endpoint... ra console của server rồi gọi next() để chuyển tiếp; Express duyệt tiếp ngăn xếp route, gặp app.delete('/users') khớp cả đường dẫn lẫn phương thức DELETE, handler này gọi res.send nên client nhận chuỗi DELETE request to the /users endpoint. "The response will be 'Accessing the /users endpoint...'" sai vì console.log chỉ ghi log phía server, không gửi gì về client. "The response will be 'GET request to the /users endpoint'." sai vì mã không có route GET nào, và request ở đây là DELETE. "The response will be 'POST request to the /users endpoint'." sai vì cũng không có route POST, chuỗi đó không xuất hiện trong mã.

Cùng chương 3

Xuất hiện trong 1 bộ đề