0
0
NestJSframework~30 mins

Controller decorator in NestJS - Mini Project: Build & Apply

Choose your learning style9 modes available
Building a Simple NestJS Controller with @Controller Decorator
📖 Scenario: You are creating a simple web server using NestJS. You want to organize your routes by creating a controller that handles HTTP requests for a specific path.
🎯 Goal: Build a NestJS controller class using the @Controller decorator to define a route prefix.
📋 What You'll Learn
Create a controller class named AppController
Use the @Controller decorator with the path 'app'
Add a method named getHello inside the controller
Use the @Get decorator on the getHello method with no path argument
Return the string 'Hello World!' from the getHello method
💡 Why This Matters
🌍 Real World
Controllers in NestJS organize routes and handle HTTP requests in web applications and APIs.
💼 Career
Understanding controllers and decorators is essential for backend development with NestJS, a popular Node.js framework.
Progress0 / 4 steps
1
Create the controller class
Create a class named AppController in your file.
NestJS
Need a hint?

Use the class keyword followed by AppController and curly braces.

2
Add the @Controller decorator with path 'app'
Add the @Controller('app') decorator above the AppController class definition. Also import Controller from @nestjs/common.
NestJS
Need a hint?

Remember to import Controller and place @Controller('app') right before the class.

3
Add a getHello method with @Get decorator
Import Get from @nestjs/common. Inside AppController, add a method named getHello decorated with @Get() that returns the string 'Hello World!'.
NestJS
Need a hint?

Use @Get() above the method and return the exact string 'Hello World!'.

4
Complete the controller export
Ensure the AppController class is exported as default export by adding export default AppController; at the end of the file.
NestJS
Need a hint?

Use export default AppController; at the end of the file to complete the export.