0
0
Node.jsframework~20 mins

Creating a basic HTTP server in Node.js - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Node.js HTTP Server Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this basic HTTP server response?
Consider this Node.js HTTP server code. What will the server send back to the client when accessed?
Node.js
import http from 'http';
const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, world!');
});
server.listen(3000);
AThe server responds with JSON: { message: 'Hello, world!' }
BThe server responds with plain text: 'Hello, world!'
CThe server responds with HTML content containing 'Hello, world!'
DThe server throws an error because of missing response end
Attempts:
2 left
💡 Hint
Look at the content type and the string passed to res.end.
📝 Syntax
intermediate
2:00remaining
Which option contains a syntax error in creating an HTTP server?
Identify the option that will cause a syntax error when creating a basic HTTP server in Node.js.
A;)0003(netsil.revres ;)} ;)'iH'(dne.ser { >= )ser ,qer((revreSetaerc.ptth = revres tsnoc ;'ptth' morf ptth tropmi
Bconst http = require('http'); const server = http.createServer((req, res) => { res.end('Hi'); }); server.listen(3000);
Cimport http from 'http'; const server = http.createServer((req, res) => { res.end('Hi'); }); server.listen(3000);
Dmport http from 'http'; const server = http.createServer((req, res) => { res.end('Hi'); }); server.listen(3000);
Attempts:
2 left
💡 Hint
Look for missing punctuation or misplaced code inside the callback.
state_output
advanced
2:00remaining
What is the value of 'count' after 3 client requests?
This server counts how many requests it has handled. What is the value of 'count' after 3 requests?
Node.js
import http from 'http';
let count = 0;
const server = http.createServer((req, res) => {
  count++;
  res.end(`Request number: ${count}`);
});
server.listen(3000);
A3
B1
C0
DUndefined
Attempts:
2 left
💡 Hint
The count variable increases each time the server handles a request.
🔧 Debug
advanced
2:00remaining
Which option causes the server to crash on request?
One of these server codes will crash when a client makes a request. Identify which one.
Aimport http from 'http'; const server = http.createServer((req, res) => { res.end('OK'); res.end('Again'); }); server.listen(3000);
Bimport http from 'http'; const server = http.createServer((req, res) => { res.end('OK'); }); server.listen(3000);
Cimport http from 'http'; const server = http.createServer((req, res) => { res.write('OK'); }); server.listen(3000);
Dimport http from 'http'; const server = http.createServer((req, res) => { res.statusCode = 200; res.end('OK'); }); server.listen(3000);
Attempts:
2 left
💡 Hint
Check if the response is properly ended.
🧠 Conceptual
expert
2:00remaining
What happens if server.listen is called twice on the same port?
Consider calling server.listen(3000) twice on the same HTTP server instance. What will happen?
AThe server listens twice on port 3000 without error.
BThe server restarts and resets all state.
CThe server silently ignores the second listen call.
DThe second call throws an error because the port is already in use.
Attempts:
2 left
💡 Hint
Ports can only be bound once per server instance.