All quizzesMedium
HTTP Servers & APIs — Series 3
Preview — 3 of 10 questions
What happens?
javascript
const server = http.createServer((req, res) => {
res.write('partial');
res.setHeader('X-Trace', 'abc');
res.end('done');
});AThe header is sent with the response, since headers are flushed at end().
BIt throws ERR_HTTP_HEADERS_SENT: the first write() flushes the status line and headers, so nothing can be added afterwards.
CThe header is silently ignored and the response completes normally.
DThe response is sent twice.
The request is GET /search?q=node+js&page=2. What is logged?
javascript
const url = new URL(req.url, `http://${req.headers.host}`);
console.log(url.pathname, url.searchParams.get('q'), url.searchParams.get('page'), typeof url.searchParams.get('page'));A/search?q=node+js&page=2 node+js 2 number
B/search node+js 2 string
C/search node js 2 number
D/search node js 2 string
Requests to /users/new hit the wrong handler. Why?
javascript
app.get('/users/:id', (req, res) => res.send(`user ${req.params.id}`));
app.get('/users/new', (req, res) => res.send('new user form'));AExpress matches routes in registration order, and /users/:id matches /users/new with id = 'new', so the specific route must be registered before the parameterised one.
B:id only matches numbers, so the problem is elsewhere.
CBoth handlers run, and the last response wins.
DExpress sorts routes by specificity automatically.
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.