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
Đề bài
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 án
Lờ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

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