Routes and Request Handling
Define API routes, handle various HTTP methods, and extract data from request bodies, parameters, and queries.
Understanding API Routes
When you build a web API, you need a way for clients (like a browser or mobile app) to send requests to specific functions on your server. This is where routes come in!
A route defines how a client's request URL (like /users or /products/1) maps to a particular action in your application code. It's like a street address for your API's functionalities.
HTTP Methods: The Action Verbs
Besides the URL, every request also uses an HTTP method, which tells the server what kind of action the client wants to perform. Common methods include:
- GET: Retrieve data (e.g., get a list of users).
- POST: Create new data (e.g., create a new user).
- PUT: Update existing data completely.
- PATCH: Partially update existing data.
- DELETE: Remove data.
NestJS uses decorators like @Get() or @Post() to link these methods to your controller functions.
All lessons in this course
- Routes and Request Handling
- CRUD Operations with TypeORM
- Error Handling and Interceptors