Basic Node.js questions
1. What is Node.js and why is it fast for I/O?
Node.js runs JavaScript on the V8 engine outside the browser. It uses a single-threaded event loop with non-blocking I/O handled by libuv, so one process can serve many concurrent connections without a thread per request.
2. What is middleware in Express?
Middleware functions receive (req, res, next) and run in order for matching routes. They handle logging, parsing bodies, authentication and errors. Calling next() passes control on; error middleware takes four arguments (err, req, res, next).
3. CommonJS vs ES modules?
CommonJS uses require and module.exports and loads synchronously. ES modules use import and export, are statically analysable, support top-level await and are the standard. Node chooses based on the file extension (.mjs/.cjs) or the type field in package.json.
Intermediate Node.js questions
4. What are streams in Node.js?
Streams process data in chunks instead of loading it all into memory. Types are readable, writable, duplex and transform. pipeline() connects them with proper error handling and backpressure, for example when uploading or downloading large files.
5. process.nextTick vs setImmediate?
process.nextTick callbacks run immediately after the current operation, before promise microtasks and before the event loop continues. setImmediate runs in the check phase after I/O callbacks. Overusing nextTick can starve I/O.
6. How do you handle errors in async code?
Use try/catch with async/await, return rejected promises, and send errors to central Express error middleware. Also handle unhandledRejection and uncaughtException to log and restart cleanly instead of continuing in a bad state.
7. How do you implement JWT authentication?
On login, verify the password hash (bcrypt) and sign a short-lived JWT with a secret. The client sends it in an Authorization header or httpOnly cookie, and middleware verifies it on each request. Use refresh tokens or session versions for revocation.
Advanced Node.js questions
8. How do you scale a Node.js application?
Run multiple processes with the cluster module or PM2, or multiple containers behind a load balancer. Keep servers stateless (sessions in Redis or a database), cache hot data, and move CPU-heavy work to worker threads or background queues.
9. What are worker threads?
worker_threads run JavaScript in parallel threads with their own event loops, sharing memory through SharedArrayBuffer or passing messages. They are for CPU-intensive tasks such as image processing or hashing, not for I/O.
10. How do you secure a Node.js API?
Validate all input (zod or Joi), use parameterized queries, set security headers (helmet), rate-limit logins, store secrets in environment variables, keep dependencies patched (npm audit), use HTTPS and apply least-privilege authorization checks on every route.
Want to practise these with a trainer? APEX live batches include mock interviews, real projects and placement support. See upcoming batches.
