Quiz: Building a Basic Web Server
Test your understanding of web servers in Node.js with this comprehensive quiz.
We'll cover the following...
1
Which statement is true about the server started by the following code?
const http = require('http');
const server = http.createServer((req, res) => {
if (req.url === '/api' && req.method === 'GET') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify({ message: 'GET received' }));
} else if (req.url.startsWith('/api') && req.method === 'POST') {
res.writeHead(201, { 'Content-Type': 'text/plain' });
res.write('POST received');
} else if (req.url === '/api/hello' && req.method === 'GET') {
res.write('Specific hello route');
} else {
res.writeHead(404);
res.write('Not Found');
}
res.end();
});
server.listen(3000);
A)
It handles all GET requests by responding with “GET received” in JSON format.
B)
It responds with “POST received” for any POST request.
C)
It responds with “Specific hello route” only for GET requests to /api/hello
.
D)
It responds with JSON for all requests to /api
.
Question 1 of 50 attempted
...
Access this course and 1400+ top-rated courses and projects.