Ex Express.js APIs

Build HTTP APIs with routing, middleware, validation, controllers, and error handling.

Lessons

Sign in to save progress
1 Express.js Tutorial 2 Express.js HOME 3 Express.js Introduction 4 What Is Express.js 5 How Express Works 6 Installing Express 7 Your First Express App 8 Express Project Structure 9 npm Scripts for Express 10 Auto-Reload with Nodemon 11 ESM vs CommonJS in Express 12 The Express app Object 13 Starting the Server with listen 14 HTTP Methods in Express 15 The Request Object 16 The Response Object 17 Response Methods 18 HTTP Status Codes 19 Sending JSON Responses 20 Sending Files and Downloads 21 Redirects and Location Headers 22 Express Routing & Controllers 23 Route Parameters 24 Query Strings 25 Multiple Route Handlers 26 Express Router Module 27 Chaining Routes with route() 28 Route Patterns and Matching 29 Nested and Mounted Routers 30 Route and Middleware Order 31 Middleware & Request Flow 32 Middleware Basics 33 Application-Level Middleware 34 Router-Level Middleware 35 Built-in Middleware 36 Third-Party Middleware 37 Serving Static Files 38 CORS in Express 39 Security Headers with Helmet 40 Request Logging with Morgan 41 Response Compression 42 Parsing Request Bodies 43 Handling Form Submissions 44 File Uploads with Multer 45 Working with Cookies 46 Sessions with express-session 47 Reading and Setting Headers 48 Content Negotiation 49 Validation, Errors & Status Codes 50 Validating with express-validator 51 Schema Validation with Joi 52 Custom Validation Logic 53 Sanitizing Input 54 Error Handling Middleware 55 Handling Async Errors 56 Handling 404 Not Found 57 Centralized Error Responses 58 Custom HTTP Error Classes 59 REST API Design 60 REST Principles 61 Naming REST Resources 62 Building CRUD Endpoints 63 Pagination 64 Filtering and Sorting 65 API Versioning 66 Consistent Response Shapes 67 Choosing Status Codes 68 Idempotency and Safe Methods 69 Template Engines 70 Rendering Views with EJS 71 Rendering Views with Pug 72 Rendering Views with Handlebars 73 Express with Databases 74 MongoDB with Mongoose 75 PostgreSQL with node-postgres 76 MySQL in Express 77 Sequelize ORM 78 Prisma ORM 79 Repository Pattern 80 Connection Pooling 81 Migrations and Seeders 82 Database Transactions 83 Auth Basics with Sessions or Tokens 84 Hashing Passwords with bcrypt 85 JWT Authentication 86 Session-Based Authentication 87 Authentication with Passport 88 OAuth and Social Login 89 Role-Based Access Control 90 Rate Limiting 91 CSRF Protection 92 Securing API Responses 93 Environment Config with dotenv 94 Configuration Management 95 Structured Logging with Winston 96 Request IDs and Tracing 97 Async/Await in Routes 98 Streaming Responses 99 Real-Time with Socket.IO 100 Server-Sent Events 101 Caching Responses 102 Caching with Redis 103 MVC Pattern in Express 104 Controllers 105 Service Layer 106 Dependency Injection 107 Modular Feature Architecture 108 Calling Third-Party APIs 109 Sending Email 110 Receiving Webhooks 111 API Docs with Swagger 112 Testing Express Apps 113 HTTP Tests with Supertest 114 Unit Testing with Jest 115 Integration Testing 116 Mocking Dependencies 117 Performance Tips 118 Clustering and Scaling 119 Running with PM2 120 Dockerizing Express 121 Deploying to Production 122 Health Checks and Readiness 123 Graceful Shutdown 124 Express with TypeScript 125 Typing Requests and Responses 126 Typed Middleware 127 Express Best Practices 128 Common Express Mistakes 129 Express Cheatsheet 130 Express Interview Questions 131 Express Quiz 132 Express Exercises 133 Express Practice Problems 134 Express Project Ideas 135 Express Syllabus 136 Express Study Plan 137 Express Bootcamp

Typing Requests and Responses

Express.js APIs Lesson 125 of 137 ~1 min read

Overview

Add types to req, res, params, and bodies.

Typing Requests and Responses is easiest to learn by reading the example, changing it, and observing the result.

Core Ideas

  • Understand what Typing Requests and Responses changes.
  • Run the example.
  • Change one value.
  • Explain the result.

Step by Step

  1. Read the Typing Requests and Responses example.
  2. Run it.
  3. Change it.
  4. Explain it.

Code Example

// npm install --save-dev typescript @types/express
import express, { Request, Response } from 'express';

interface Lesson { slug: string; title: string; }

const app = express();

app.get('/api/lessons/:slug', (req: Request, res: Response) => {
  const lesson: Lesson = { slug: req.params.slug, title: 'Typed lesson' };
  res.json(lesson);
});

app.listen(3000);

Checklist

  • Read the example and change one value.
  • Check the result in the browser.
  • Write down the rule you learned.

Common Mistakes

  • Skipping the example.
  • Changing many things at once.
  • Not checking the result.

Practice Challenge

Practice the Typing Requests and Responses example in a small scratch file, then explain what changed and why.

Mastery Check

  • You can explain Typing Requests and Responses.
  • You can change the example.
  • You can debug the result.
Create a free account to save which lessons you've finished. Save my progress