Node.
js Theoretical Concepts for Intermediate Interviews
1. What is Node.js?
Node.js is an open-source, cross-platform runtime environment that allows JavaScript to be run on the server side. It is
built on Chrome's V8 JavaScript engine and is designed for building scalable, event-driven, and non-blocking I/O
applications like APIs, real-time services, and microservices.
2. Event Loop
Node.js uses a single-threaded event loop to handle multiple client requests efficiently. Instead of creating new threads
for each request (like in traditional servers), it registers callbacks and handles them asynchronously, making it
lightweight and fast.
3. Non-blocking I/O
Node.js operates on a non-blocking I/O model, meaning it doesn't wait for operations like file reading or database calls
to complete. Instead, it continues executing other tasks and uses callbacks, Promises, or async/await to process the
result later.
4. Single-threaded Architecture
Despite handling many requests concurrently, Node.js uses a single thread for execution, supported by an underlying
thread pool (provided by the libuv library) for executing tasks like file system access or networking in the background.
5. Modules in Node.js
Node.js supports modular programming using:
- CommonJS modules: Uses require() and module.exports
- ES Modules (ESM): Uses import and export, enabled via "type": "module" in package.json
Modules allow code to be split into reusable components.
6. Built-in Core Modules
Node.js comes with many core modules:
- fs: File system operations (read, write files)
- http / https: Create servers or make requests
- path: Work with file paths
- os: Get system info
- url: Parse and format URLs
- crypto: Encrypt/decrypt data
- events: Implement custom event emitters
- stream: Handle continuous data flow (e.g., video, files)
7. npm (Node Package Manager)
npm is used to install third-party packages, manage dependencies, and run scripts. A project's metadata and
dependencies are stored in package.json. node_modules/ contains all installed packages.
Node.js Theoretical Concepts for Intermediate Interviews
8. Express.js Framework
Express.js is a minimal and flexible web framework built on Node.js for creating web applications and APIs.
Key features:
- Middleware support
- Route handling (GET, POST, etc.)
- Error handling
- Template engine support
9. Middleware in Express
Middleware functions have access to the req, res, and next objects. They are executed in sequence for every request
and can:
- Modify request/response objects
- Terminate request-response cycles
- Call the next middleware
10. Routing in Express
Routing defines how the app responds to HTTP requests. Each route is associated with an endpoint and an HTTP
method.
11. Asynchronous Programming
Node.js relies heavily on async programming for performance.
Methods:
- Callbacks
- Promises
- Async/Await
12. EventEmitter
EventEmitter is a class in Node.js to create and handle custom events.
13. File System (fs) Module
fs allows working with files:
- fs.readFile (async)
- fs.writeFile
- fs.existsSync (sync)
14. Streams in Node.js
Streams are used to process data piece-by-piece, ideal for large files.
Types:
- Readable
Node.js Theoretical Concepts for Intermediate Interviews
- Writable
- Duplex (both read/write)
- Transform (modify data)
15. Buffer
A Buffer is a raw binary data storage (like an array of bytes) used when dealing with streams and file systems.
16. Child Processes
Node can run other programs/scripts using:
- exec(): Executes command in shell and buffers output
- spawn(): Stream data piece-by-piece
- fork(): Creates a separate Node.js process (used for clustering)
17. Environment Variables
Used to store config outside the source code using .env files and dotenv package.
18. Error Handling
Use try/catch with async/await, or pass errors to Express middleware via next(err).
19. Authentication (Basic)
- Use JWT (JSON Web Tokens) for token-based authentication.
- Use bcrypt for password hashing.
- Middleware can be used to protect routes.
20. Deployment Tools
- Use PM2 to manage Node apps in production.
- Docker for containerization.
- Use NGINX as a reverse proxy.
- Host on platforms like Heroku, Vercel, or AWS.
21. Logging & Debugging
- Use console.log() for simple logs.
- Use libraries like winston, morgan for advanced logging.
- Debug with node inspect or Chrome DevTools.
22. Clustering
Used to utilize multi-core systems by running multiple Node processes.
Sample Interview Questions
- What is the difference between require and import?
- How does Node handle multiple requests?
- What is the purpose of middleware in Express?
Node.js Theoretical Concepts for Intermediate Interviews
- What is the Event Loop in Node.js?
- How do you handle async code in Node.js?