How to setup Node js project?
- Install Node.js
- First, visit the Node.js official website.
- Download and install the setup node js project based on your system (Windows, Mac, etc.).
- Initialize the Project
Run the following command in your terminal:
sh
npm init
- Press Enter multiple times and then type yes to complete the process.
- This will create a package.json file inside your project.
- What is package.json?
- This file is generated by npm (Node Package Manager).
- It contains an object called dependencies, which lists all installed third-party libraries along with their versions.
Example:
sh
“ts-node”: “^2.3.0”
- The ^ symbol means that when you run npm update, this library will get updated automatically. If you remove this symbol, the version will remain fixed.
Example of package.json
- All installed libraries are listed under the dependencies object.
- There is also a devDependencies object.
What are devDependencies?
- It includes libraries that are only needed during development but are not required in production.
Understanding Development & Production Environments
- Development Environment
- Used for testing on a local PC or a testing environment.
- Example: The database used during testing is different from the one used by the public.
- Production Environment
- This is the live environment used by actual users.
What is Frontend & Backend?
- When a mobile app or website sends a request (e.g., login request with email and password) to the server, the backend (Node.js) connects to the MongoDB database.
- After fetching data, the server either returns the requested data or an error.
- Frontend
- The code running on a mobile app or website.
- Backend
- The Node.js code runs on a separate server (not on your local PC).
How Do Frontend & Backend Communicate?
- They communicate using APIs (Application Programming Interface).
- Just like we use a GUI (Graphical User Interface) to interact with a computer or mobile device using buttons,
applications use APIs to communicate with other applications.
Example:
- You can use Google authentication to log in using a Gmail ID.
- This happens using an API provided by Google.
Conclusion:
A good software is not just popular because it has many users, but also because it allows other applications to interact with it using APIs.