Node.js Middle interview questions and answers
75 questions for a Node.js (Middle) interview - with correct answers, explanations and links to the docs.
Take this quiz- 1.Which list shows the main phases of the Node.js event loop in order?
- 2.What is the order of the output?
- 3.Inside an I/O callback (for example, in
fs.readFile), which runs first? - 4.Why can recursive
process.nextTickcalls be dangerous? - 5.What does the libuv thread pool handle by default?
- 6.A request handler runs a CPU-heavy loop for 2 seconds. What happens to other requests?
- 7.Which of these can block the event loop?
- 8.How do you measure how busy the event loop is?
- 9.What does
timer.unref()do? - 10.What does
await setTimeout(1000)imported fromnode:timers/promisesdo? - 11.Why use streams to send a 2 GB file instead of
readFile? - 12.What are the four basic types of streams in Node.js?
- 13.What is backpressure in streams?
- 14.Why is
stream.pipeline()preferred over chaining.pipe()? - 15.How can you read a readable stream line by line or chunk by chunk with
async/await? - 16.What is a
Buffer? - 17.What is the difference between
Buffer.alloc(10)andBuffer.allocUnsafe(10)? - 18.What does
Buffer.from("привіт").lengthreturn? - 19.What is
objectModein streams? - 20.How do you gzip a file with streams?
- 21.What happens if an
EventEmitteremitserrorand there is noerrorlistener? - 22.What does the warning
MaxListenersExceededWarningusually point to? - 23.How do you wait for one event as a promise?
- 24.What is the difference between operational errors and programmer errors?
- 25.Why is it useful to pass
causewhen re-throwing an error? - 26.What is
AbortControllerused for in Node.js? - 27.Why should every outgoing HTTP call have a timeout?
- 28.How do you define error-handling middleware in Express?
- 29.What changed in Express 5 for
asyncroute handlers? - 30.Does the order of
app.use()calls matter in Express? - 31.What is
express.Router()used for? - 32.What is a key feature of Fastify compared with Express?
- 33.Why validate request input on the server even if the front end already validates it?
- 34.Which HTTP methods are idempotent by definition?
- 35.What is a good way to return API errors in a consistent format?
- 36.How do you serve a folder of static files in Express?
- 37.How should passwords be stored?
- 38.What does a JWT signature guarantee?
- 39.What is a common downside of JWTs compared with server-side sessions?
- 40.Which cookie attributes help protect a session cookie?
- 41.Why must SQL queries use parameters instead of string concatenation?
- 42.What does the
helmetmiddleware do? - 43.Why add rate limiting to a login endpoint?
- 44.How do you generate a cryptographically secure random token?
- 45.How should you compare a secret token from a request with the stored one?
- 46.Why do Node.js apps use a database connection pool?
- 47.What is the N+1 query problem?
- 48.When do you need a database transaction?
- 49.What is an ORM such as Prisma or TypeORM?
- 50.Which Node.js version feature lets you use SQLite without installing a package?
- 51.What are worker threads for?
- 52.What is the difference between
child_process.execandchild_process.spawn? - 53.What does
child_process.fork()do? - 54.What does the
clustermodule do? - 55.How do you mock a function with the built-in test runner?
- 56.How do you test an Express app's endpoints without starting a real server on a fixed port?
- 57.Which
node:assertfunction compares objects by their contents? - 58.What does the Twelve-Factor App recommend for configuration?
- 59.What should a Node.js server do when it receives
SIGTERM? - 60.Why should logs include a request ID?
- 61.Which data must never be written to logs?
- 62.In a Dockerfile for a Node.js app, why copy
package.jsonand the lock file before the source code? - 63.What is a health check endpoint like
/healthused for? - 64.What does the
exportsfield inpackage.jsondo? - 65.Can CommonJS code
require()an ES module in modern Node.js? - 66.What does the
enginesfield inpackage.jsonexpress? - 67.What are npm workspaces?
- 68.What is a risk of
postinstallscripts in dependencies? - 69.When are Server-Sent Events (SSE) a better choice than WebSockets?
- 70.What is a challenge of WebSockets with several server instances?
- 71.Why use a job queue like BullMQ for sending emails after sign-up?
- 72.What is
AsyncLocalStorageused for? - 73.Why does HTTP keep-alive matter for a service that calls other APIs a lot?
- 74.Why is cursor-based pagination often better than
OFFSETfor large tables? - 75.What is the cache-aside pattern with Redis?