More in Backend Dev — page 7
res.send vs res.json vs res.end
WHAT IT TESTS: how Express sends responses. OUTLINE: send is flexible and sets content type by type, json serializes and sets JSON content type, end is the raw http terminator with no body helpers. RED FLAG: using res.end to return an object or claiming they.
Parsing JSON bodies with express.json
WHAT IT TESTS: why req.body needs a body parser. OUTLINE: register express.json via app.use so it reads the request stream and populates req.body before handlers run, place it before routes. RED FLAG: expecting req.body to work without any parser.
Query params vs route params in Express
WHAT IT TESTS: distinguishing two ways to pass data in a URL. OUTLINE: query string values via req.query.q, named path segments via req.params.id, query is optional, route params are part of the matched pattern. RED FLAG: mixing up req.query and req.params.
Serving static files in Express
WHAT IT TESTS: knowing the built-in static middleware. OUTLINE: app.use with express.static pointing at the public directory, files served relative to that root, often combined with an absolute path.
Minimal Express Hello World server
WHAT IT TESTS: basic Express setup fluency. OUTLINE: import express, create an app, define app.get on the root sending a response, call app.listen on a port. RED FLAG: forgetting app.listen or confusing the require with the app instance.
http.Agent and connection pooling
WHAT IT TESTS: reusing TCP connections for outbound requests. OUTLINE: the agent pools and keeps sockets alive, avoiding repeated TCP and TLS handshakes, controlled by keepAlive and maxSockets. RED FLAG: thinking each request always needs a fresh connection.
Backpressure in Node.js streams
WHAT IT TESTS: flow control between fast producer and slow consumer. OUTLINE: backpressure pauses the readable when the writable buffer fills, pipe and pipeline manage it automatically, pipeline also propagates errors and cleans up.
Scaling across cores with cluster and os
WHAT IT TESTS: scaling single-threaded Node across cores. OUTLINE: os.cpus gives core count, the primary forks one worker per core, all workers share the listening port, the OS load-balances connections. RED FLAG: thinking one Node process uses all cores.
fs.watch vs fs.watchFile
WHAT IT TESTS: file change detection mechanisms. OUTLINE: watch uses OS event notifications, efficient but inconsistent across platforms, watchFile polls stat at an interval, reliable but slower. RED FLAG: not knowing watch is event based versus polling.
path.resolve vs path.join
WHAT IT TESTS: nuance of path construction. OUTLINE: join concatenates and normalizes relative segments, resolve builds an absolute path right to left from cwd and resets on any absolute segment. RED FLAG: treating them as interchangeable.
Reading a POST body from the request stream
WHAT IT TESTS: that req is a readable stream. OUTLINE: body arrives in chunks via data events, accumulate them, on the end event concatenate and JSON.parse inside try/catch. RED FLAG: expecting req.body to exist or parsing before all chunks arrive.
Counting lines in a 5GB log file efficiently
WHAT IT TESTS: streaming versus buffering large data. OUTLINE: readFile loads all 5GB into RAM and may exceed buffer limits, instead stream with createReadStream plus readline and count line by line. RED FLAG: proposing readFile then split on newlines.
Minimal HTTP server with the http module
WHAT IT TESTS: knowing the raw http API beneath frameworks. OUTLINE: createServer with a request listener, set status and Content-Type, end the response, call listen on 3000. RED FLAG: forgetting res.end so the connection hangs.
Why use path.join over string concatenation
WHAT IT TESTS: awareness of cross-platform path handling. OUTLINE: path.join uses the correct OS separator, collapses duplicate slashes, normalizes . and .. segments. RED FLAG: hardcoding forward slashes and assuming concatenation always works.
fs.readFileSync vs fs.readFile
WHAT IT TESTS: grasp of blocking vs non-blocking I/O. OUTLINE: sync blocks the event loop and returns directly, async takes a callback or promise, only sync is safe at startup. RED FLAG: using readFileSync inside a request handler.
Async iterators and for await...of for streaming
WHAT IT TESTS: streaming vs buffering everything. OUTLINE: async iterators yield values lazily over time; for await...of consumes them sequentially with backpressure, keeping memory bounded.
Promise.all vs Promise.allSettled
WHAT IT TESTS: choosing fail-fast vs collect-all. OUTLINE: all rejects on the first failure; allSettled always fulfills with a status/value or reason per input. Use allSettled when partial success is acceptable.
Bounded concurrency for many async requests
WHAT IT TESTS: limiting concurrency, not just running parallel. OUTLINE: chunk the array and await Promise.all per chunk, or run a fixed worker pool pulling from a shared index; cap in-flight requests. RED FLAG: firing all 1000 at once or going fully serial.
Comparing the three async error-handling styles
WHAT IT TESTS: fluency across async error styles. OUTLINE: callbacks pass err as first arg; Promises route errors to catch; async/await uses try/catch; an unhandled rejection can crash the Node process.
Handling async errors in Express middleware
WHAT IT TESTS: routing async errors into Express. OUTLINE: await inside try/catch and call next(err) on failure, or wrap the handler in an async error adapter; never let a rejected Promise go uncaught.