DEV Community

Cover image for How to make a simple GET request or an API endpoint in Nestjs?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

How to make a simple GET request or an API endpoint in Nestjs?

Originally posted here!

To make a simple GET request or an API endpoint in Nestjs, there are 3 things we need to define such as:

  • a valid typescript class to write various request methods definitions, in our case we will be writing only for the GET request method.
  • a @Controller() decorator function before the controller class to define the metadata for the route.
  • a @Get() decorator function to set the metadata to define that a specific method in the class is used for a particular GET request.

TL;DR

import { Controller, Get } from "@nestjs/common";

// the @Controller() decorator function will instruct Nestjs
// to add a route of `/greet`
@Controller("greet")
export class GreetControler {
  // the @Get() decorator function will instruct Nestjs
  // that this is the default method that should be
  // invoked when the user requests a `GET` to `/greet` endpoint
  @Get()
  sayHello() {
    return `Hello World`;
  }
}
Enter fullscreen mode Exit fullscreen mode

NOTE: To generate the basic boilerplate code with the main app module, see the blog on How to create a new project or an app in Nestjs? if you have not made a Nestjs project.

For example, let's say we need to make a simple GET request API endpoint called /greet and on requesting to this endpoint it should return a string response called Hello World!.

So to do this first let's make a file to write the GET request code, let's name it greet.controller.ts.

To make this file in the terminal you can use the mkdir command to make a directory called greet and then use the touch command to quickly make a controller file.

It can be done like this,

mkdir greet && touch greet/greet.controller.ts
Enter fullscreen mode Exit fullscreen mode

After running the above command, you will have a structure that looks something like this,

- greet
 - greet.controller.ts
Enter fullscreen mode Exit fullscreen mode

Now inside the greet.controller.ts typescript file let's make a class called GreetControler and export it since we will need to import this class in the main app module later.

It can be done like this,

export class GreetControler {}
Enter fullscreen mode Exit fullscreen mode

After defining the controller class and exporting it, now we need to import the @Controller() decorator function from the @nestjs/common module like this,

import { Controller } from "@nestjs/common";

export class GreetControler {}
Enter fullscreen mode Exit fullscreen mode

Now let's use the @Controller() decorator function before the GreetControler class and pass the string of greet to it. This is done because the @Controller() decorator function accepts a parameter to define the route metadata to be used. Since we need the route to be /greet, we can just pass the greet string to it and Nestjs will handle the routing mechanism.

It can be done like this,

import { Controller } from "@nestjs/common";

// makes the /greet route
@Controller("greet")
export class GreetControler {}
Enter fullscreen mode Exit fullscreen mode

After using the @Controller() decorator function to define the route, let's now make a method in the class called sayHello. This method will be used to return the response.

It can be done like this,

import { Controller } from "@nestjs/common";

// makes the /greet route
@Controller("greet")
export class GreetControler {
  // a simple method that returns a string
  sayHello() {
    return `Hello World`;
  }
}
Enter fullscreen mode Exit fullscreen mode

Now we need to instruct Nestjs that when the request method is a GET and the route is /greet we need to run the sayHello method, to do that let's use the @Get() decorator function from the @nestjs/common module and use it before the sayHello method without any arguments like this,

import { Controller, Get } from "@nestjs/common";

// the @Controller() decorator function will instruct Nestjs
// to add a route of `/greet`
@Controller("greet")
export class GreetControler {
  // the @Get() decorator function will instruct Nestjs
  // that this is the default method that should be
  // invoked when the user requests a `GET` to `/greet` endpoint
  @Get()
  sayHello() {
    return `Hello World`;
  }
}
Enter fullscreen mode Exit fullscreen mode

NOTE: If you look at the @Get() decorator function you can see that we are not passing any arguments to the function, this is because we are not defining any nested routes for the /greet endpoint. For example, if you need this method to be invoked for the /greet/hai endpoint, you need to pass a string of hai to the @Get() decorator function.

The code for the /greet controller is done. The only thing left is to import our GreetControler controller class and then initialize it as a controller in the main app module.

If you navigate to the app.module.ts (This is the default filename used if you have created the Nestjs project using the Nestjs CLI), there you need to import the GreetControler class and add it as a controller in the controllers array in the @Module() decorator function.

It can be done like this,

/* app.module.ts file */
import { Module } from "@nestjs/common";

// Greet controller
import { GreetControler } from "./greet/greet.controller";

@Module({
  imports: [],
  controllers: [GreetControler], // <- add controller here
  providers: [],
})
export class AppModule {}
Enter fullscreen mode Exit fullscreen mode

Now we have made a simple GET method /greet endpoint successfully in the Nestjs project. Yay 🥳!

If you navigate to the https://localhost:3000/greet from your browser, you can see the response of Hello World from the server.

See the above code live in codesandbox.

That's all 😃!

Feel free to share if you found this useful 😃.


Top comments (0)