After running `npx express-generator myapp` and starting the generated app with `npm start`, what is the response when a GET request is made to `http://localhost:3000/users`? Assume the default Express Generator template is unmodified and the `users.js` route file contains:
4 phương ánCó giải thích
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
res.send('respond with a resource');
});
module.exports = router;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: "respond with a resource". Lần theo mã: app.js do express-generator sinh ra có dòng app.use('/users', usersRouter), gắn router của users.js vào tiền tố /users; trong router, router.get('/') khớp phần đường dẫn còn lại sau tiền tố, nên GET http://localhost:3000/users rơi đúng vào handler này và res.send gửi chuỗi respond with a resource với mã 200, kiểu text/html. "404 status with 'Cannot GET /users'" chỉ xảy ra khi không có route nào khớp, trong khi mẫu mặc định đã gắn sẵn usersRouter. "{ message: 'Users route' }" sai vì handler gửi chuỗi chứ không gửi object JSON, và nội dung đó không có trong mã. "Throws an error due to missing route" sai vì route tồn tại và chạy bình thường, server khởi động ở cổng 3000 theo tệp bin/www.
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