Câu hỏi: SDN302 Chương 3 - Express.js và Middleware
24 câu trắc nghiệm kèm lời giải của môn Server-Side development with NodeJS, Express, and MongoDB, xếp theo thứ tự nhập vào ngân hàng.
Danh sách câu hỏi
app.run(middleware)app.use(middleware)app.middleware(middleware)app.listen(middleware)using Express.js. Identify which routes handle GET and POST requests. app.get('/users', ...);
app.post('/users', ...);app.listen(3000, ...) (Choose 2 answers)
Để xác định các đường dẫn xử lý GET và POST yêu cầu, chúng ta cần xem xét các dòng mã sau: app.get('/users', ...); app.post('/users', ...). Trong đó, app.get() được sử dụng để xử lý GET yêu cầu, trong khi app.post() được sử dụng để xử lý POST yêu cầu. Điều này có nghĩa là đường dẫn /users sẽ xử lý cả GET và POST yêu cầu. Vì vậy, hai đáp án đúng là: "The /users endpoint handles GET requests." và "The /users endpoint handles POST requests."
app.get('/', (req, res) => {
res.send('Hello, this is a GET request');
}); app.listen(3000, ...). What is right URL to display the text "Hello, this is a GET request"?
return when called with an invalid user ID? app.get('/user/:id', (req, res) => {
const userId = req.params.id; if (!isValidId(userId)) {
res.status(400).send("Invalid ID");
}
else {
res.status(200).send("User details");
}
});
for defining a route and starting the server. const express = require('express');const app = express();
app.get('/', (req, res) => {
res.send('Hello, world!');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
}); (Choose 2 answers)
Để trả lời câu hỏi này, chúng ta cần hiểu rõ về cách sử dụng Express.js. Đầu tiên, chúng ta cần nhập thư viện Express.js bằng cách sử dụng câu lệnh require('express'). Sau đó, chúng ta tạo một đối tượng app bằng cách sử dụng express(). Đối tượng này sẽ được sử dụng để định nghĩa các route và bắt đầu server. Trong ví dụ này, chúng ta sử dụng app.get('/') để định nghĩa một route cho URL '/' và gửi phản hồi 'Hello, world!' lại cho người dùng. Cuối cùng, chúng ta sử dụng
app.listen(3000, () => {
...
}) để bắt đầu server trên cổng 3000 và in ra thông báo 'Server is running on port 3000' khi server đã được bắt đầu. Vì vậy, câu trả lời chính xác là 'The
app.get('/', (req, res) => {
...
}) statement defines the route' và 'The
app.listen(3000, () => {
...
}) statement starts the server'.
app.get('/') sends 'Welcome to the homepage!';
app.get('/about') sends 'About us';a final app.use sends 404 'Page not found'; app.listen on 3000) (Choose 3 answers)
app.get('/', (req, res) => {
res.send('Hello, this is a GET request');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
}); What is right URL to display the text "Hello, this is a GET request"?
reqresappnextif the user visits /welcome? app.get('/welcome', (req, res) => {
res.send("Hello, Express!");
});
Trong Node với Express, `app.use(express.json())` có tác dụng giải quyết các yêu cầu JSON. Khi một yêu cầu được gửi đến ứng dụng Express, nó sẽ được kiểm tra xem có chứa dữ liệu JSON hay không. Nếu có, Express sẽ giải quyết dữ liệu đó thành một đối tượng JavaScript có thể sử dụng. Điều này cho phép bạn dễ dàng truy cập và xử lý dữ liệu JSON trong ứng dụng của mình. Ví dụ, nếu bạn có một yêu cầu POST với một cơ thể JSON như sau:
{
"name": "John",
"age": 30
}, Express sẽ giải quyết nó thành một đối tượng như sau:
{
name: 'John',
age: 30
}. Điều này giúp bạn dễ dàng truy cập và xử lý dữ liệu JSON trong ứng dụng của mình.
app.get('/', (req, res) => {
throw new Error('Test error');
});
app.listen(3000);if (!req.headers['x-auth']) {
return next(new Error('Missing auth header'));
}next(); };
app.use(checkAuth);
app.get('/api/data', (req, res) => {
res.json({
data: 'Secret'
}
);
});
app.use((err, req, res, next) => {
res.status(401).json({
error: err.message
}
);
}); app.listen(3000); Assume the request has no `x-auth` header.