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
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 ánLờ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
______ is a fast, unopinionated and minimalist web framework for Node.jsCó lời giảiCâu hỏiHow do you add middleware to an Express application?Có lời giảiCâu hỏiConsider the following code snippet using Express.js. Identify which routes handle GET and POST requests. app.get('/users', ...); app.post('/users', ...); app.listen(3000, ...) (Choose 2 answers)Có lời giảiCâu hỏiCả 38 câu của chủ đề38 câu