Skip to content

Latest commit

 

History

History
184 lines (143 loc) · 10.5 KB

File metadata and controls

184 lines (143 loc) · 10.5 KB

Express JS Full Course

Disclaimer: This is a personal summary and interpretation based on a YouTube video. It is not official material and not endorsed by the original creator. All rights remain with the respective creators.

This document summarizes the key takeaways from the video. I highly recommend watching the full video for visual context and coding demonstrations.

Before You Get Started

  • I summarize key points to help you learn and review quickly.
  • Simply click on Ask AI links to dive into any topic you want.

AI-Powered buttons

Teach Me: 5 Years Old | Beginner | Intermediate | Advanced | (reset auto redirect)

Learn Differently: Analogy | Storytelling | Cheatsheet | Mindmap | Flashcards | Practical Projects | Code Examples | Common Mistakes

Check Understanding: Generate Quiz | Interview Me | Refactor Challenge | Assessment Rubric | Next Steps

Introduction to Express.js

Summary: Express.js is a popular Node.js framework for building web APIs, used in millions of projects. It's easy to learn, unopinionated, and allows quick setup without overhead. Companies from startups to Fortune 500 use it for its simplicity and performance.

Key Takeaway/Example: Express handles server-side logic, receiving HTTP requests from clients (like browsers or apps) and sending responses. For an e-commerce site, the client requests product lists via HTTP, and the server processes and returns data—like a waiter taking orders to the kitchen.

Link for More Details: Ask AI: Introduction to Express.js

Setting Up an Express Project

Summary: Start by creating a directory, initializing with npm init -y, installing Express and Nodemon (as dev dependency). Use ES modules by setting "type": "module" in package.json. Create src/index.mjs, import Express, instantiate the app, and listen on a port (e.g., 3000 or process.env.PORT).

Key Takeaway/Example: Run the app with npm run start:dev for watch mode. Test by visiting localhost:3000 in a browser—it shows "Cannot GET /" initially since no routes are defined.

import express from 'express';
const app = express();
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Running on port ${port}`));

Link for More Details: Ask AI: Setting Up an Express Project

Defining Routes and GET Requests

Summary: Routes are paths in your app (e.g., /api/users). Use app.get() to handle GET requests, passing the path and a handler function with req and res objects. Send responses with res.send(), and set status with res.status().

Key Takeaway/Example: For a base route, send "Hello World" or JSON. Create routes like /api/users to return an array of mock users. Check network tab for status codes.

app.get('/', (req, res) => res.send('Hello World'));
app.get('/api/users', (req, res) => res.send([{ id: 1, username: 'anson', displayName: 'Anson' }]));

Link for More Details: Ask AI: Defining Routes and GET Requests

Using Route Parameters

Summary: Route params allow dynamic values in paths (e.g., /api/users/:id). Access via req.params.id. Validate (e.g., parseInt), find matching data, and handle errors like invalid ID (400) or not found (404).

Key Takeaway/Example: Use a mock users array. Parse ID, find user, return it or error.

app.get('/api/users/:id', (req, res) => {
  const parsedId = parseInt(req.params.id);
  if (isNaN(parsedId)) return res.status(400).send('Bad request. Invalid ID');
  const findUser = mockUsers.find(user => user.id === parsedId);
  if (!findUser) return res.sendStatus(404);
  res.send(findUser);
});

Link for More Details: Ask AI: Using Route Parameters

Handling Query Parameters

Summary: Query strings (?key=value) add data to GET requests for filtering/sorting. Access via req.query. Destructure params like filter and value, then filter arrays accordingly. If params missing, return full data.

Key Takeaway/Example: Filter users by substring in username or displayName.

app.get('/api/users', (req, res) => {
  const { query: { filter, value } } = req;
  if (filter && value) return res.send(mockUsers.filter(user => user[filter].includes(value)));
  return res.send(mockUsers);
});

Link for More Details: Ask AI: Handling Query Parameters

POST Requests and Request Bodies

Summary: Use POST for creating resources. Enable body parsing with app.use(express.json()). Handle in app.post(), validate/generate ID, push to array or save to DB, return 201 with new resource.

Key Takeaway/Example: Use Thunder Client or Postman to send JSON bodies. For users, add ID from last array item +1.

app.use(express.json());
app.post('/api/users', (req, res) => {
  const newUser = { ...req.body, id: mockUsers[mockUsers.length - 1].id + 1 };
  mockUsers.push(newUser);
  return res.status(201).send(newUser);
});

Link for More Details: Ask AI: POST Requests and Request Bodies

Validation with Express-Validator

Summary: Install express-validator for request validation. Use middleware like body('field').isLength() in routes. Check validationResult(req), return errors (400) or proceed.

Key Takeaway/Example: Validate username, displayName, password lengths and types.

import { body, validationResult } from 'express-validator';
router.post('/api/users', [body('username').trim().isString().isLength({ min: 3 })], (req, res) => {
  const errors = validationResult(req);
  if (!errors.isEmpty()) return res.status(400).json({ errors: errors.array() });
  // Proceed
});

Link for More Details: Ask AI: Validation with Express-Validator

Authentication with Passport.js

Summary: Set up Passport for local auth: install passport, passport-local, express-session. Configure strategies, serialize/deserialize users. Protect routes with middleware.

Key Takeaway/Example: Use LocalStrategy for username/password. Hash passwords with bcrypt.

passport.use(new LocalStrategy(async (username, password, done) => {
  const user = await User.findOne({ username });
  if (!user || !await bcrypt.compare(password, user.password)) return done(null, false);
  return done(null, user);
}));

Link for More Details: Ask AI: Authentication with Passport.js

Unit Testing with Jest

Summary: Install Jest, set up scripts. Mock dependencies (e.g., express-validator, models). Test handlers for cases like validation errors, success, DB failures.

Key Takeaway/Example: Mock req/res, assert status/send calls.

jest.mock('express-validator');
test('should return 400 on validation error', async () => {
  // Mock setup and assertions
});

Link for More Details: Ask AI: Unit Testing with Jest

Integration and E2E Testing with Supertest

Summary: Install Supertest. Separate test DB, use beforeAll/afterAll for connect/drop. Test flows: create user, login, access protected routes.

Key Takeaway/Example: Chain requests, send bodies, assert status/body.

import request from 'supertest';
it('should create user and login', async () => {
  const res = await request(app).post('/api/users').send({ username: 'test' });
  expect(res.statusCode).toBe(201);
});

Link for More Details: Ask AI: Integration and E2E Testing with Supertest


About the summarizer

I'm Ali Sol, a Backend Developer. Learn more: