كورس node js في ٣ ساعات | شرح نظري و تطبيقي | Node JS - MongoDB - Express - API - Postman
Table of Contents
Introduction
This tutorial provides a comprehensive overview of Node.js, MongoDB, Express, and API development, as presented in a three-hour crash course. By following these steps, you'll gain a solid understanding of backend development using these technologies, enabling you to build your own applications.
Step 1: Understanding Node.js
-
What is Node.js?
- Node.js is a JavaScript runtime built on Chrome's V8 engine, allowing you to run JavaScript on the server side.
-
Key Features
- Non-blocking, event-driven architecture.
- Ideal for building scalable network applications.
Step 2: Basic Concepts of Web Applications
- How Websites Work
- Client-server model: The client (browser) sends requests to the server, which responds with data.
- Frontend vs. Backend: Frontend is what users see; backend handles data processing and storage.
Step 3: Setting Up Node.js
-
Installation
- Download Node.js from the official website.
- Follow the installation instructions for your operating system.
-
Creating Your First Node.js Application
- Open your terminal and create a new directory:
mkdir my-node-app cd my-node-app
- Initialize a new Node.js project:
npm init -y
- Create an
index.js
file and add the following code to start a basic server:const http = require('http'); const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World\n'); }); server.listen(3000, () => { console.log('Server running at http://localhost:3000/'); });
- Run your application:
node index.js
- Open your terminal and create a new directory:
Step 4: Using Postman for API Testing
-
Installation
- Download Postman from the official website.
-
Making Requests
- Open Postman and create a new request.
- Set the request type (GET, POST, etc.) and enter your API endpoint (e.g.,
http://localhost:3000
). - Click "Send" to test your API.
Step 5: Building a Simple API
- Creating Routes with Express
- Install Express:
npm install express
- Create an
app.js
file and set up a basic Express server:const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => { res.send('Hello World from Express!'); }); app.listen(port, () => { console.log(`Server running at http://localhost:${port}/`); });
- Install Express:
Step 6: Introduction to EJS for HTML Rendering
- Setting Up EJS
- Install EJS:
npm install ejs
- Set EJS as the view engine in your Express app:
app.set('view engine', 'ejs');
- Create a views directory with an
index.ejs
file:<h1>Hello from EJS!</h1>
- Render the view in your route:
app.get('/', (req, res) => { res.render('index'); });
- Install EJS:
Step 7: Working with MongoDB
-
Introduction to MongoDB
- MongoDB is a NoSQL database that stores data in JSON-like documents.
-
Connecting to MongoDB
- Install the MongoDB driver:
npm install mongodb
- Connect to your MongoDB database and perform basic CRUD operations.
- Install the MongoDB driver:
Step 8: Deploying Your Application
- Using Render for Deployment
- Sign up for Render and create a new web service.
- Link your GitHub repository and configure environment variables as needed.
Conclusion
By following these steps, you have laid the foundation for building applications using Node.js, Express, and MongoDB. You can further explore advanced topics like authentication, middleware, and more complex database operations. Experiment with your own projects and continue learning to enhance your development skills.