Web Development with Coding
Backend with Node.js
In This Section, You Will Learn:
- • What backend development is
- • Node.js – JavaScript on the server
- • Express.js – the most popular framework
- • REST APIs – how frontend talks to backend
- • Creating your first API endpoints
- • Handling requests and responses
What is Backend Development?
- The backend is everything the user DOESN'T see.
- Backend Handles:
- • User authentication (login/signup)
- • Database operations (saving/fetching data)
- • Business logic (calculations, validations)
- • File uploads
- • Payment processing
- • Email sending
- Frontend vs Backend:
- • Frontend: 'Show the login form'
- • Backend: 'Check if password is correct'
- • Frontend: 'Display user profile'
- • Backend: 'Fetch user data from database'
- Full-stack = Frontend + Backend = Build complete applications
Node.js – JavaScript on Servers
- Node.js lets you run JavaScript OUTSIDE the browser.
- Why Node.js:
- • Same language (JS) for frontend AND backend
- • Very fast (non-blocking I/O)
- • Huge ecosystem (npm packages)
- • Used by: Netflix, LinkedIn, PayPal, Uber
- Setting Up:
- 1. Install Node.js from nodejs.org
- 2. Create a project folder
- 3. Run: npm init -y
- 4. Create index.js
- Simple Node Server:
- “`
- const http = require('http');
- const server = http.createServer((req, res) => {
- res.end('Hello World!');
- });
- server.listen(3000);
- // Visit: http://localhost:3000
- “`
Express.js – The Popular Framework
- Express makes building APIs MUCH easier than raw Node.
- Installing Express:
- “`
- npm install express
- “`
- Basic Express Server:
- “`
- const express = require('express');
- const app = express();
- app.get('/', (req, res) => {
- res.send('Hello World!');
- });
- app.listen(3000, () => {
- console.log('Server running on port 3000');
- });
- “`
- Key Express Features:
- • Routing (handle different URLs)
- • Middleware (process requests)
- • Static files (serve images, CSS)
- • Easy JSON handling
REST APIs – How Data Moves
- REST API = Rules for how frontend and backend communicate.
- HTTP Methods:
- • GET: Fetch data
- • POST: Create new data
- • PUT/PATCH: Update existing data
- • DELETE: Remove data
- Example API Routes:
- “`
- // Get all users
- app.get('/api/users', (req, res) => {…});
- // Get one user
- app.get('/api/users/:id', (req, res) => {…});
- // Create user
- app.post('/api/users', (req, res) => {…});
- // Update user
- app.put('/api/users/:id', (req, res) => {…});
- // Delete user
- app.delete('/api/users/:id', (req, res) => {…});
- “`
- JSON: Data format used for API communication
Handling Requests and Responses
- Request (req): What the client sends
- Response (res): What the server returns
- Getting Data from Requests:
- “`
- // URL parameters: /users/123
- app.get('/users/:id', (req, res) => {
- const userId = req.params.id;
- });
- // Query strings: /search?q=hello
- app.get('/search', (req, res) => {
- const query = req.query.q;
- });
- // Body data (POST)
- app.use(express.json());
- app.post('/users', (req, res) => {
- const { name, email } = req.body;
- });
- “`
- Sending Responses:
- “`
- res.json({ message: 'Success' });
- res.status(404).json({ error: 'Not found' });
- “`