0
0
NestJSframework~20 mins

Why controllers handle incoming requests in NestJS - See It in Action

Choose your learning style9 modes available
Why Controllers Handle Incoming Requests in NestJS
📖 Scenario: You are building a simple web server using NestJS. You want to organize your code so that it can respond to users' requests properly.
🎯 Goal: Learn how to create a controller in NestJS that handles incoming HTTP GET requests and sends back a response.
📋 What You'll Learn
Create a controller class named AppController
Add a method named getHello inside AppController
Use the @Controller() decorator on the class
Use the @Get() decorator on the getHello method
Return the string 'Hello World!' from getHello
💡 Why This Matters
🌍 Real World
Web servers use controllers to respond to user requests like loading pages or submitting forms.
💼 Career
Understanding controllers is essential for backend developers working with NestJS or similar frameworks to build APIs and web services.
Progress0 / 4 steps
1
Create the Controller Class
Create a class called AppController and decorate it with @Controller().
NestJS
Need a hint?

The @Controller() decorator marks the class as a controller that can handle requests.

2
Add a Method to Handle GET Requests
Inside AppController, add a method named getHello and decorate it with @Get().
NestJS
Need a hint?

The @Get() decorator tells NestJS this method handles GET requests.

3
Return a Response String
Make the getHello method return the string 'Hello World!'.
NestJS
Need a hint?

Returning a string sends it as the HTTP response body.

4
Complete the Controller Setup
Ensure the AppController class is exported and properly decorated with @Controller() and the getHello method is decorated with @Get() returning 'Hello World!'.
NestJS
Need a hint?

This is the complete controller that handles incoming GET requests and returns a response.