1. What is Node.js and how does it work?
Node.js is a runtime environment that allows you to run JavaScript on the server side. It's built on
Chrome's V8 engine, uses an event-driven, non-blocking I/O model, and operates on a single thread
using an event loop.
2. What are the key features of Node.js?
Asynchronous and event-driven, single-threaded but scalable, fast execution using V8, no buffering,
and cross-platform compatibility.
3. What is the event loop in Node.js?
The event loop handles all async operations in Node.js. It lets Node perform non-blocking I/O
operations on a single thread by offloading operations to the system kernel whenever possible.
4. What is non-blocking I/O and how is it handled in Node.js?
Non-blocking I/O allows Node.js to handle multiple operations without waiting for any to complete.
It's handled through callbacks, promises, and the event loop.
5. What is the difference between synchronous and asynchronous programming?
Synchronous blocks execution until the current task finishes, while asynchronous allows other
operations to run while waiting for a task to complete.
6. What is the difference between setTimeout, setImmediate, and process.nextTick?
setTimeout schedules a callback after a delay, setImmediate runs after the current I/O cycle, and
process.nextTick runs after the current operation completes but before the event loop continues.
7. What is a callback function?
A function passed into another function as an argument and executed later, often used for async
operations.
8. What is a Promise?
A Promise represents a value that may be available now, later, or never. It avoids callback hell and
provides better error handling.
9. What is async/await?
Syntactic sugar over Promises that allows writing asynchronous code that looks synchronous, using
try/catch for error handling.
10. What is middleware in Express.js?
Middleware is a function that has access to req, res, and next, used for logging, authentication, error
handling, etc.
11. How do you handle errors in Node.js?
Using callbacks with error arguments, try/catch with async/await, and global handlers like
process.on('uncaughtException').
12. What is the difference between require() and import?
require is CommonJS (sync), import is ES Module (async). ES Modules are more modern and
standardized.
13. What is the purpose of the package.json file?
It stores metadata about the project including dependencies, scripts, entry point, and more.
14. What is the difference between dependencies and devDependencies?
dependencies are required in production, devDependencies are only needed during development
(e.g., testing tools).
15. What are streams in Node.js?
Streams are objects that let you read or write data in chunks instead of loading the entire file in
memory.
16. What is the difference between Buffers and Streams?
Buffers store entire data in memory; streams process data in chunks, making them more memory
efficient.
17. What is EventEmitter in Node.js?
A core module that enables the creation and handling of custom events using on and emit methods.
18. What is CORS and how do you handle it in Express?
CORS allows or blocks requests from other domains. Handled in Express with the cors middleware.
19. How do you manage environment variables in Node.js?
Using dotenv package and storing variables in a .env file which are accessed via process.env.
20. How do you create a basic REST API using Express?
Set up Express, define routes with app.get/post/put/delete, and start the server using app.listen.
21. What is clustering in Node.js and why is it used?
Clustering allows Node.js to utilize multiple CPU cores by forking the server process, enhancing
scalability.
22. What is the role of the V8 engine in Node.js?
V8 compiles JavaScript to native machine code, providing fast execution and memory management.
23. How do you use the fs module to read/write files asynchronously?
Use fs.readFile and fs.writeFile with callbacks or fs/promises for async/await usage.
24. How do you secure Node.js applications?
Use HTTPS, Helmet for headers, input validation, JWTs, rate limiting, and avoid eval or insecure
code.
25. What is the difference between CommonJS and ES Modules?
CommonJS uses require/module.exports; ES Modules use import/export. ESM is asynchronous and
modern standard.