Consider the following code snippet using Express.js. Identify which routes handle GET and POST requests.
4 phương ánCó giải thích
const express = require('express');
const app = express();
app.get('/users', (req, res) => {
res.send('GET request to the /users endpoint');
});
app.post('/users', (req, res) => {
res.send('POST 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 gồm hai ý: "The /users endpoint handles GET requests." và "The /users endpoint handles POST requests.". Lần theo mã: app được tạo từ express(), dòng app.get('/users', …) đăng ký một handler chỉ chạy khi request dùng phương thức GET tới /users và trả chuỗi GET request to the /users endpoint; dòng app.post('/users', …) đăng ký handler thứ hai cho cùng đường dẫn nhưng chỉ khớp phương thức POST; cuối cùng app.listen(3000) mở cổng 3000. Express khớp route theo cả phương thức lẫn đường dẫn, nên một URL có thể có nhiều handler khác nhau. "The /users endpoint handles PUT requests." sai vì không có app.put nào, request PUT tới /users rơi xuống xử lý mặc định và nhận 404 với thông báo Cannot PUT /users. "The /users endpoint handles DELETE requests." sai tương tự vì thiếu app.delete, request DELETE cũng nhận 404.
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