Node.js Tutorial: From Basic to Advance
Everything you need to become a Node.js Developer
🚀 Introduction to Node.js
Node.js is an open-source, cross-platform JavaScript runtime environment that allows you to run JavaScript code outside a web browser. It's widely used for building scalable network applications.
⚙️ Installation & Setup
Download Node.js from nodejs.org. Use the terminal/command prompt to check installation:
node -v
npm -v
📦 Node.js Core Modules
- fs – File system operations
- http – Create HTTP servers
- https – HTTPS servers with SSL/TLS
- path – Handle and transform file paths
- url – Parse and format URLs
- querystring – Parse and stringify URL query strings
- events – Event-driven programming with EventEmitter
- stream – Streaming data processing (read/write)
- os – Operating system-related utility methods
- crypto – Cryptographic operations (hashing, encryption)
- zlib – Compression and decompression (gzip, deflate)
- util – Utility functions (promisify, inherits, etc.)
- timers – Scheduling functions like setTimeout, setInterval
- dns – DNS lookup and hostname resolution
- net – Low-level networking (TCP/IPC)
- child_process – Spawn and manage child processes
- process – Provides info and control over the current Node process
- readline – Read data from readable streams line by line
- buffer – Handling binary data
- console – Console logging functions (console.log, etc.)
- v8 – Interact with the V8 engine internals
- assert – Assertion testing for validating invariants
- cluster – Run multiple instances of Node.js for load balancing
- tty – Support for terminal I/O (used in REPL)
- repl – Read-Eval-Print Loop for interactive console
- module – Internal module loader (advanced use)
📁 Recommended Project Structure
project/
├── node_modules/
├── public/
├── src/
│ ├── controllers/
│ ├── models/
│ ├── routes/
│ ├── middlewares/
│ └── utils/
├── .env
├── package.json
└── server.js
This structure helps keep the code organized and scalable.
🌐 Express.js Framework
Express is a minimal and flexible Node.js web application framework that provides a robust set of features to develop web and mobile applications.
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello from Express!');
});
app.listen(3000);
🧩 Middleware in Node.js
Middleware functions are functions that have access to the request object, the response object, and the next middleware function in the application’s request-response cycle.
🔐 Authentication & Authorization
Authentication can be handled using libraries like passport.js
or jsonwebtoken
(JWT). Implement secure routes and protect sensitive data.
📁 File Uploads in Node.js
Use libraries like multer
to handle file uploads. It helps parse multipart/form-data
requests.
📡 Real-Time Communication with Socket.io
Build real-time features like chat apps using socket.io
that leverages WebSockets.
🗄️ Using Databases (MySQL, MongoDB)
Connect Node.js to databases using:
- MongoDB: via Mongoose
- MySQL: via mysql2 or sequelize ORM
💡 Best Practices
- Use environment variables with
dotenv
- Validate input with
Joi
orzod
- Use try-catch with async/await
- Follow proper folder structure
- Write unit tests using
Jest
orMocha
❓ Node.js Interview Questions (Basic to Advanced)
- What is Node.js and how does it work?
- Explain the event-driven architecture of Node.js.
- What is the difference between synchronous and asynchronous programming?
- What are Node.js core modules? Name a few.
- How does the event loop work in Node.js?
- What is npm and what is it used for?
- How do you create a simple HTTP server in Node.js?
- What is Express.js and why is it used?
- Explain middleware in Express.js.
- How do you handle file uploads in Node.js?
- What is a callback function and how is it used in Node?
- What is the purpose of `require()` in Node.js?
- How can you connect Node.js to a MongoDB or MySQL database?
- Explain error handling in Node.js.
- What is JWT and how is it used in authentication?
- What is the difference between `process.nextTick()`, `setImmediate()`, and `setTimeout()`?
- How does clustering work in Node.js?
- How would you secure a Node.js application?
- What are Streams in Node.js?
- How does real-time communication work with Socket.io?
🍃 MongoDB Interview Questions (Basic to Advanced)
- What is MongoDB and how is it different from SQL databases?
- What are Documents and Collections in MongoDB?
- What is BSON?
- How do you create a database and collection in MongoDB?
- Explain CRUD operations in MongoDB with examples.
- What is the _id field in MongoDB?
- How do indexes work in MongoDB?
- What is aggregation in MongoDB?
- Difference between $match, $group, and $project in aggregation pipeline?
- What are embedded documents and when to use them?
- How do you perform joins in MongoDB?
- What is a replica set?
- What is sharding and why is it used?
- Difference between find() and findOne()
- How do you perform pagination in MongoDB?
- What are schema validations in MongoDB?
- Explain the role of MongoDB Atlas.
- How do you secure MongoDB in production?
- What is the difference between MongoDB and Mongoose?
- What are transactions in MongoDB and how are they used?
🚀 Express.js Interview Questions
- What is Express.js and why is it used?
- How do you install and set up Express?
- What are the main features of Express.js?
- What is middleware in Express.js?
- Difference between application-level and router-level middleware?
- How do you define routes in Express?
- How do you handle 404 and error middleware in Express?
- What are the different HTTP methods supported by Express?
- How do you parse incoming request bodies?
- How can you handle file uploads in Express?
- How to connect a MongoDB/Mongoose model in Express?
- What is the use of `express.static()`?
- How to handle route parameters and query strings?
- What is `req` and `res` in Express?
- How do you implement JWT authentication in Express?
- How to structure a large Express application?
- What are CORS and how do you enable them in Express?
- How do you protect routes in Express?
- How can you create and use routers in Express?
- Difference between `app.use()` and `app.get()`?
🧠 Node.js Advanced Interview Questions (5+ Years Experience)
- Explain the Node.js event loop in detail. How does it handle async I/O?
- How does Node.js achieve non-blocking I/O using libuv?
- What are the performance bottlenecks in a Node.js application and how do you resolve them?
- How would you scale a Node.js application across multiple CPU cores?
- What is clustering in Node.js and when should you use it?
- How do you handle memory leaks in Node.js?
- How would you implement caching in a high-traffic Node.js API?
- What are Streams in Node.js and how do they improve performance?
- How do you secure a Node.js REST API in production?
- What are worker threads and when should you use them over clustering?
- Explain the difference between process.nextTick(), setImmediate(), and setTimeout().
- How would you implement rate limiting in a Node.js application?
- What’s the difference between synchronous and asynchronous error handling in Node.js?
- How do you implement logging in a production Node.js app?
- What is load balancing and how can it be used with Node.js apps?
- Explain the architecture of your last enterprise-level Node.js project.
- What tools do you use for profiling and monitoring Node.js apps in production?
- How do you ensure graceful shutdown in a Node.js app?
- How does garbage collection work in Node.js?
- What are some anti-patterns in Node.js and how do you avoid them?
✅ Conclusion
Node.js is powerful for building backend services, APIs, real-time applications, and even automation scripts. Continue exploring modules, tools, and best practices to become a full-stack Node.js developer.