Server-Side development with NodeJS, Express, and MongoDB
Câu hỏi của môn
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)
db.orders.aggregate([ {
$group: {
_id: "$status", total: {
$sum: "$amount"
}
}
}]);
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"?