What is the response when a POST request is made to `http://localhost:3000/api/users` with the body `{ "name": "Alice", "email": "[email protected]" }` in the following Express application? const express = require('express'); const mongoose = require('mongoose'); const app = express();
4 phương ánCó giải thíchDễ
app.use(express.json());
mongoose.connect('mongodb://localhost:27017/test', {
useNewUrlParser: true
}); const userSchema =
new mongoose.Schema({
name: String, email: String
}); const User =
mongoose.model('User', userSchema);
app.post('/api/users', async (req, res) => {
try {
const user = await User.create(req.body); res.status(201).json(user);
}
catch (err) {
res.status(400).json({
error: err.message
}
);
}
});
app.listen(3000);Các phương án trả lời
Chọn một phương ánLời giảiChọn phương án để mở
Khi thực hiện yêu cầu POST đến địa chỉ http://localhost:3000/api/users với cơ thể yêu cầu là { "name": "Alice", "email": "[email protected]" } trong ứng dụng Express này, ứng dụng sẽ tạo một tài khoản mới trong cơ sở dữ liệu MongoDB dựa trên dữ liệu được gửi. Nếu quá trình tạo tài khoản thành công, ứng dụng sẽ trả về trạng thái 201 Created và thông tin về tài khoản mới được tạo, bao gồm ID, tên và địa chỉ email. Nếu quá trình tạo tài khoản gặp lỗi, ứng dụng sẽ trả về trạng thái 400 Bad Request và thông điệp lỗi.
Cùng chương 5
Which of the following are true about the match option in the populate() method? (Choose 2 answers)Có lời giảiCâu hỏiWhich of the following are valid options for the populate() method in Mongoose? (Choose 2 answers)Có lời giảiCâu hỏiWhat are not use cases for Mongoose in Node.js?Có lời giảiCâu hỏiCả 34 câu của chủ đề34 câu