Câu hỏi: SDN302 Server-Side development with NodeJS, Express, and MongoDB – Bộ ôn tập thi lại số 2
50 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 đúng thứ tự trong đề.
Danh sách câu hỏi
const express = require('express');
const exphbs = require('express-handlebars');
const app = express();
app.engine('handlebars', exphbs({ defaultLayout: false }));
app.set('view engine', 'handlebars');
app.get('/profile', (req, res) => {
res.render('profile', { name: 'Charlie', hobbies: ['reading', 'gaming'] });
});
app.listen(3000);
// views/profile.handlebars
<div>
<h1>{{name}}</h1>
<ul>
{{#each hobbies}}
<li>{{this}}</li>{{/each}} </ul> </div>
<div>
<h1>Charlie</h1>
<ul>
<li>reading</li>
<li>gaming</li>
</ul>
</div><div>
<h1>Charlie</h1>
<ul></ul>
</div><div>
<h1>undefined</h1>
<ul>
<li>reading</li>
<li>gaming</li>
</ul>
</div>Đáp án đúng: "
<div>
<h1>Charlie</h1>
<ul>
<li>reading</li>
<li>gaming</li>
</ul>
</div>". Lần theo mã: app.engine đăng ký express-handlebars cho đuôi handlebars với defaultLayout false, nghĩa là không bọc layout ngoài, và app.set chọn nó làm view engine. Khi GET tới /profile, res.render tìm tệp views/profile.handlebars và truyền dữ liệu gồm name là Charlie, hobbies là mảng reading, gaming. Trong template, {{name}} được thay bằng Charlie; khối each lặp qua mảng hobbies, và trong mỗi lượt {{this}} là phần tử hiện tại, nên sinh ra hai thẻ li là reading và gaming. Cách gọi exphbs() trực tiếp như hàm là kiểu của các bản express-handlebars cũ (bản mới dùng exphbs.engine()), câu hỏi xét kết quả render với cấu hình chạy được này. "<div> <h1>Charlie</h1> <ul></ul> </div> " chỉ xảy ra khi hobbies rỗng hoặc không được truyền, trong khi dữ liệu có hai phần tử. Kết quả có <h1>undefined</h1> sai vì name đã được truyền, và kể cả khi thiếu, Handlebars in chuỗi rỗng chứ không in chữ undefined. "Throws an error due to invalid Handlebars syntax" sai vì mọi thẻ đều đúng cú pháp và khối each được đóng đầy đủ.
const http = require('http');
const fs = require('fs');
http.createServer((req, res) => {
if (req.method === 'GET') {
fs.readFile('file.txt', (err, data) => {
if (err) throw err;
res.end(data);
});
}
}).listen(8080);const http = require('http');
const fs = require('fs');
http.createServer((req, res) => {
if (req.method === 'POST') {
fs.readFile('file.txt', (err, data) => {
res.end(data);
});
}
}).listen(8080);const http = require('http');
http.createServer((req, res) => {
if (req.method === 'GET') {
res.end('File content');
}
}).listen(8080);const http = require('http');
const fs = require('fs');
http.createServer((req, res) => {
if (req.method === 'GET') {
fs.writeFileSync('file.txt', 'Hello');
res.end('File written');
}
}).listen(8080);Đáp án đúng: "const http = require('http'); const fs = require('fs');
http.createServer((req, res) => {
if (req.method === 'GET') {
fs.readFile('file.txt', (err, data) => {
if (err) throw err; res.end(data);
}
);
}
}).listen(8080);". Lần theo mã: server tạo bằng http.createServer lắng nghe cổng 8080; khi request có req.method bằng GET, fs.readFile đọc file.txt bất đồng bộ, callback nhận data (Buffer chứa nội dung tệp) và res.end(data) gửi nội dung đó về client rồi đóng phản hồi; nếu đọc lỗi thì ném err. Đây là đoạn duy nhất vừa kiểm tra đúng phương thức GET vừa thực sự đọc tệp để trả về. Đoạn kiểm tra req.method === 'POST' sai vì nó chỉ phục vụ POST, request GET sẽ không bao giờ được trả lời và treo tới khi hết thời gian chờ. Đoạn gửi res.end('File content') sai vì nó trả một chuỗi cố định viết sẵn, không đọc tệp nào. Đoạn gọi fs.writeFileSync('file.txt', 'Hello') sai vì nó ghi đè tệp rồi trả chuỗi File written, tức là thay đổi dữ liệu thay vì trả nội dung tệp, đi ngược tính an toàn của GET.
const userSchema = new mongoose.Schema({
name: String,
age: Number
});Đáp án đúng: "A schema structure for documents". Đoạn mã gọi new mongoose.Schema với hai trường, name kiểu String và age kiểu Number, tức là mô tả hình dạng mà mỗi document người dùng sẽ có; Mongoose dùng nó để ép kiểu (cast) và kiểm tra dữ liệu trước khi lưu. Bản thân userSchema chưa làm việc gì với cơ sở dữ liệu. "A model definition" sai vì model chỉ có khi đem schema biên dịch bằng mongoose.model('User', userSchema); khi đó mới gọi được find hay save. "A middleware function" sai vì middleware (hook) của Mongoose được gắn lên schema bằng userSchema.pre hoặc userSchema.post với một hàm, còn đoạn này chỉ khai báo trường. "A MongoDB query" sai vì truy vấn là lời gọi như User.find với bộ lọc, gửi yêu cầu tới máy chủ; khai báo schema không gửi gì tới MongoDB.
fs.readFilefs.readSyncfs.readFileSyncfs.readexpress.urlencoded()express.static()express.json()express.parse()deleteManyremoveDocumentsdeleteOneremoveManyconst express = require('express');
const app = express();
app.all('/users', (req, res, next) => {
console.log('Accessing the /users endpoint...');
next(); // pass control to the next handler
});
app.delete('/users', (req, res) => {
res.send('DELETE request to the /users endpoint');
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});