Introduction to Node js
01 What is Node.js?
- Node.js is an open-source platform – It allows JavaScript to run on a server.
- It is free to use – Anyone can use it without paying.
- Works on multiple operating systems – Supports Windows, Linux, macOS, and more.
- Uses JavaScript on the server – Helps in building fast and scalable web applications.
Simple Example to Understand:
- Imagine you want to cook food.
- A kitchen is needed because it provides space and tools for cooking.
- The kitchen is not the food, but it helps you prepare it.
- Similarly, Node.js is not a programming language, but it provides an environment to run JavaScript outside the browser.
02 What is Express.js?
- Express.js is a web framework for Node.js – It makes building web and mobile applications easier.
- It is minimal and flexible – Provides essential tools without unnecessary complexity.
- Helps in running Node.js as a server – Makes handling requests and responses simpler.
- Without Express.js, using Node.js directly can be difficult – Express provides ready-made features that save time and effort.
Simple Example to Understand:
- Imagine you want to travel a long distance.
- You can walk, but it will be slow and tiring.
- Instead, you take a bus, which makes the journey faster and more comfortable.
- Similarly, Node.js alone is like walking, while Express.js is like the bus that makes server-side development easier and faster.
03 What is MongoDB?
- MongoDB is a document-based database – It stores data in a flexible and scalable way.
- Uses collections and documents – A collection holds multiple documents (similar to tables in SQL).
- Schema-less database – You can store any type of data inside any collection.
- Powerful querying and indexing – Helps in retrieving data efficiently.
How Does It Work?
- Suppose you want to store users and their posts in a database.
- You will create two collections:
- Users Collection → Stores user details.
- Posts Collection → Stores posts written by users.
- Each user and post is saved as a separate document inside its respective collection.
What is Schema-less & Why Is It a Problem?
- MongoDB doesn’t enforce a strict structure – It allows storing any data inside any collection.
- Example: You can accidentally store product data inside the Users Collection, which can cause issues.
Solution – Using Mongoose
- Mongoose is a library that helps define a schema for MongoDB.
- It tells MongoDB:
- “Hey! A user should only have name, email, and password fields!”
- This ensures only valid data is stored in the correct collection.
- What is Mongoose?
04 What is Mongoose?
- Mongoose is an ODM (Object Document Mapper) for MongoDB – It helps in managing data easily.
- What is ODM? – ODM maps objects in code to documents in a database, just like how ORM (Object Relational Mapper) maps objects to tables in SQL databases.
- Why do we need Mongoose? – MongoDB is schema-less, meaning it allows storing any data in any collection.
- Mongoose helps by defining a schema – It ensures that each collection stores only the correct type of data.
Example to Understand:
- Imagine you are running a school and have a Student Register.
- You want each student’s entry to have only a Name, Age, and Class.
- Without rules, someone might enter extra information like phone number or address, making records messy.
- Mongoose works like a strict register – It makes sure only Name, Age, and Class are recorded in the Student Register.
How Does Mongoose Help?
- Creates a schema – Defines which fields a document should have.
- Ensures data consistency – Prevents incorrect or unnecessary data from being stored.
Makes querying easy – Provides built-in functions to interact with MongoDB.