Express + TypeScript के साथ REST API
In this page:
import express, { Request, Response } from "express";
app.post("/path", (req: Request<Params, ResBody, ReqBody>, res: Response<ResBody>) => {
// req.body is checked against ReqBody
});
Core Concept
Express और TypeScript के साथ एक REST API बनाने का मतलब है अपने route handlers के request और response objects को type करना, इसलिए req.body.email access करना एक declared shape के against checked होता है, silently any होने के बजाय।
उदाहरण: Core Concept
// import { Request, Response } from "express";
interface CreateUserBody { email: string; }
// app.post("/users", (req: Request<{}, {}, CreateUserBody>, res: Response) => {
// console.log(req.body.email);
// });
console.log("req.body is checked against a declared shape, not any");
Basic Setup
एक basic setup express के साथ @types/express install करता है, और typed handlers Request<Params, ResBody, ReqBody> generics उपयोग करते हैं यह describe करने के लिए कि हर route पर exactly क्या expected है।
उदाहरण: Basic Setup
// npm install express
// npm install --save-dev @types/express
console.log("@types/express enables Request<Params, ResBody, ReqBody> generics");
Typed Example
एक typed example: app.post('/users', (req: Request<{}, {}, CreateUserBody>, res: Response<UserResponse>) => { ... }) ensure करता है कि handler का req.body CreateUserBody के against checked हो और इसके res.json(...) calls UserResponse के against checked हों।
उदाहरण: Typed Example
interface CreateUserBody { email: string; }
interface UserResponse { id: number; email: string; }
// app.post('/users', (req: Request<{}, {}, CreateUserBody>, res: Response<UserResponse>) => {
// res.json({ id: 1, email: req.body.email });
// });
console.log("Both req.body and res.json() are checked against declared types");
Project Usage
एक असली project में, इन request/response interfaces को एक frontend client के साथ (एक shared-types package के ज़रिए) share करना API contract को दोनों ends पर enforced रखता है बिना किसी side के silently drift होने के।
उदाहरण: Project Usage
interface UserResponse { id: number; email: string; }
// Shared with a frontend client via a shared-types package.
const response: UserResponse = { id: 1, email: "[email protected]" };
console.log(response);
Best Practices
अपने compile-time types के साथ runtime validation जोड़ें (Zod या Joi जैसी एक library से), क्योंकि TypeScript के checks runtime पर गायब हो जाते हैं और एक untrusted client से एक malformed request body को रोक नहीं सकते।
उदाहरण: Best Practices
interface CreateUserBody { email: string; }
function isCreateUserBody(value: any): value is CreateUserBody {
return typeof value?.email === "string";
}
// Runtime validation (e.g. Zod/Joi) catches what compile-time types can't.
console.log(isCreateUserBody({ email: "[email protected]" }));
- Body को type किए बिना
req.body.emailपढ़ना, इसलिए यहanyहै और एक typo unnoticed रह जाता है। app.use(express.json())भूल जाना, इसलिएreq.bodyruntime परundefinedहै भले ही यह type-check हो।Request<CreateUserBody>लिखना जब body third generic है,Request<{}, {}, CreateUserBody>।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Todo App with TypeScript
- REST API with Express + TypeScript
- React Dashboard with TypeScript
- CLI Tool with TypeScript
- Library with TypeScript
- Full Stack TypeScript App
- TypeScript Design System
- TypeScript Monorepo Project
- Authentication System
- Real-time App with Socket.io
- GraphQL API with TypeScript
- Microservices with TypeScript
- TypeScript Best Practices Review
- What to Learn Next