Lesson 2
Controllers in NestJS
Welcome to Controllers in NestJS

Welcome back! So far, we've covered how to set up your first NestJS application and understand its basic structure. Now, it's time to delve deeper into the backbone of your application — the controllers. In this lesson, you'll learn what controllers are, how to create them, and why they're essential in a NestJS environment.

What Are Controllers?

A controller in NestJS is responsible for handling incoming HTTP requests and returning a response to the client. Controllers are a crucial part of developing server-side applications as they define how different endpoints respond to client requests. They act as the entry point to your application's functionality, coordinating between the client requests and the business logic encapsulated in services.

Controllers are typically used to manage routing, meaning they determine which function to call when a specific URL is requested by a client. By structuring your application with controllers, you ensure that your code is modular, testable, and easy to maintain.

Routing

Let's take a look at how routing works by exploring an example with the CorgiController.

First, we define a new controller by creating a file named corgi.controller.ts:

TypeScript
1import { Controller, Get, Render } from '@nestjs/common'; 2 3@Controller('corgi') 4export class CorgiController { 5 @Get() 6 @Render('index') 7 showCorgis() { 8 return { message: 'Corgis are the best!' }; 9 } 10}

The @Controller('corgi') decorator defines a controller class in NestJS. It tells NestJS that this particular class will handle routes that start with /corgi. Within this controller, you can define various methods that correspond to different endpoints.

For example, in the CorgiController, we use the @Get() decorator to indicate that the showCorgis() method should handle GET requests for the /corgi route. This means that when someone navigates to /corgi, this method will be called.

To render a view in response to this GET request, we use the @Render('index') decorator. This tells NestJS to use the index.hbs template file to generate the HTML that will be sent back to the client. The showCorgis() method itself returns an object containing a message, which is then passed to the template for rendering. The message is displayed dynamically in the view, providing the final output to the user.

Registering the Controller

After defining the CorgiController, NestJS needs to be informed about its existence; otherwise, the controller won't function. To make this happen, you must register the controller in the AppModule by adding it to the controllers array.

While we'll dive deeper into how modules work in a future unit, for now, just know that registering your controllers in the AppModule is essential for them to handle requests within your application.

View Template

Now, let’s look at the views/index.hbs template that will render the message:

HTML, XML
1<!DOCTYPE html> 2<html> 3<head> 4 <title>Welcome</title> 5</head> 6<body> 7 <h1>{{message}}</h1> 8</body> 9</html>

This template uses Handlebars syntax to dynamically display the message passed from the controller. The {{message}} in the view corresponds directly to the message property in the object returned by the showCorgis() method in the CorgiController.

In our example, showCorgis() returns { message: 'Corgis are the best!' }, so when this view is rendered, the placeholder {{message}} will be replaced with "Corgis are the best!" in the final HTML output. This connection between the controller and the view is what allows NestJS to serve dynamic content based on the logic in your application.

Why It Matters

Getting the basics right is crucial because controllers are the backbone of your NestJS application. Understanding how to properly handle requests and route them within your application ensures that your code is modular, maintainable, and scalable. As we move forward, the fundamentals you’ve learned here will be essential as we build more complex features and structures in NestJS. Mastering these basics will make it much easier to tackle more advanced topics in the next course.

Ready to put your knowledge into practice? Let’s move to the practice section and apply what you’ve just learned!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.