Skip to content
Laviyx Learn

Try

Node.js and Express course menu

Intermediate Node.js Interview Questions and Answers

Intermediate Node.js and Express interview questions with answers: the event loop phases, process.nextTick vs setImmediate, streams and backpressure, EventEmitter, error handling in async code and Express 5, REST design, validation, SQL injection, authentication with JWT and sessions, and CORS.

Intermediate4 min readUpdated
On this page

These questions check that you understand how Node.js behaves and can build a sound API. Back answers with a short example and a reason.

The runtime#

1. Describe the phases of the event loop.#

Show solution

Each turn visits: timers (setTimeout/setInterval callbacks that are due), pending callbacks (some deferred system callbacks), poll (wait for and run I/O callbacks), check (setImmediate callbacks), and close callbacks. Between every callback, Node runs the process.nextTick queue and then promise microtasks. That is why promise callbacks run before the next timer, and why setImmediate runs before a 0 ms timer inside an I/O callback.

2. process.nextTick vs setImmediate vs setTimeout(fn, 0)?#

Show solution

process.nextTick runs right after the current operation, before promise callbacks and before the event loop continues (overusing it can starve I/O). setImmediate runs in the check phase of the current loop turn. setTimeout(fn, 0) runs in the timers phase of a later turn. Inside an I/O callback, setImmediate always fires before setTimeout(..., 0); at the top level of a script their order is not guaranteed.

3. What are streams, and what is backpressure?#

Show solution

Streams process data in chunks instead of loading it all into memory: readable (file reads, requests), writable (file writes, responses), duplex (sockets) and transform (gzip). Backpressure happens when a writable destination cannot keep up with a readable source; the writer signals "slow down" (write() returns false) and the reader pauses until drain. pipeline() handles backpressure and error cleanup automatically, which is why it is preferred over .pipe().

4. What happens if an EventEmitter emits 'error' with no listener?#

Show solution

Node throws the error, which crashes the process if nothing catches it. Always attach an error listener to emitters that can fail: streams, sockets, servers and your own emitters.

5. How do you handle errors in async/await code?#

Show solution

Wrap await calls in try/catch, or let them propagate to a central handler. Unhandled promise rejections terminate the process in current Node versions. In Express 5, errors thrown (or rejected) in async route handlers are forwarded automatically to error-handling middleware (err, req, res, next); in Express 4 you had to catch them and call next(err).

Building APIs#

6. What makes an API RESTful?#

Show solution

Resources identified by URLs (plural nouns: /api/orders/42), standard HTTP methods for actions (GET reads, POST creates, PUT replaces, PATCH updates, DELETE removes), stateless requests (each carries what the server needs, such as a token), meaningful status codes, and representations such as JSON. Filtering, sorting and pagination go in the query string.

7. PUT vs PATCH?#

Show solution

PUT replaces the whole resource with the representation sent, so all fields are required, and repeating it gives the same result (idempotent). PATCH applies a partial update, changing only the fields sent.

8. How do you prevent SQL injection?#

Show solution

Never build SQL by concatenating user input. Use parameterised queries / prepared statements (WHERE email = ? with the value passed separately), or an ORM or query builder that parameterises for you. Placeholders make the database treat input purely as data, so ' OR '1'='1 cannot change the query.

9. Why validate on the server if the front end already validates?#

Show solution

Anyone can call the API directly with curl or scripts, bypassing the front end. The server must validate types, ranges, formats and permissions for every request. Schema libraries such as Zod or Joi make this consistent and also strip unexpected fields.

10. JWT vs sessions for authentication?#

Show solution

With sessions, the server stores session data and the client holds a random session id in a cookie; revoking access is instant (delete the session), but the server needs shared storage (such as Redis) when it scales. With JWTs, the token itself carries signed claims and the server stores nothing; it scales easily, but tokens cannot be revoked before they expire without extra infrastructure, so they should be short-lived (with refresh tokens). Either way, hash passwords with bcrypt, scrypt or Argon2.

11. What is CORS?#

Show solution

Cross-Origin Resource Sharing: browsers block a page on one origin from reading responses from another origin unless the server allows it with Access-Control-Allow-* headers. For requests that are not "simple", the browser first sends a preflight OPTIONS request. Configure CORS on the API with the specific origins you trust; CORS does not restrict non-browser clients such as curl.

Key takeaway#

Next: Advanced Node.js Interview Questions.

Finished this lesson?

Node.js and ExpressAdvanced Node.js Interview Questions and AnswersAdvanced Node.js and back-end interview questions with answers: libuv and the thread pool, worker threads vs cluster, memory leaks and profiling, scaling Node across cores and machines, graceful shutdown, caching, rate limiting, message queues, security, observability, and designing a scalable API.Advanced5 min readNode.js and ExpressBeginner Node.js Interview Questions and AnswersCommon Node.js and Express interview questions for freshers with clear answers and examples: what Node.js is, single-threaded non-blocking I/O, npm and package.json, modules, require vs import, process.env, the fs module, Express basics, middleware and status codes.Beginner4 min readNode.js and ExpressThe Node.js Event Loop Explained (with Examples)Understand how Node.js runs asynchronous code: the call stack, the event loop and its phases, timers, setImmediate and process.nextTick, promise microtasks, why blocking code freezes a server, running async work in parallel with Promise.all, and worker threads for CPU-heavy tasks.Intermediate9 min readNode.js and ExpressError Handling in Express 5: Async Errors and Error MiddlewareHandle errors properly in Express: how Express 5 catches errors thrown in async route handlers, writing error-handling middleware with four arguments, custom error classes with status codes, JSON 404 and 500 responses, logging without leaking details, and handling uncaught errors in Node.js.Intermediate10 min read