How to make a simple API endpoint that catches all requests with any methods in Nestjs?
Tags: resource
Published: 2022-02-03 00:00:00

---
  title: How to make a simple API endpoint that catches all requests with any methods in Nestjs?
  published: true
  tags: Resource
  date: Thu Feb 03 2022 05:30:00 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/make-simple-api-endpoint-catches-all-requests-with-any-methods-nestjs
  cover_image: https://melvingeorge.me/_next/static/images/main-5697d3ab9e9ba2fa4cd5e2018d2b0880.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/make-simple-api-endpoint-catches-all-requests-with-any-methods-nestjs)
  
To make a simple API endpoint that catches all the requests with any methods in Nestjs, there are 3 things we need to define such as:

- a valid typescript class to write request methods definitions, in our case we will be writing only for a single endpoint that catches all the requests.
- a `@Controller()` decorator function before the controller class to define the metadata for the route.
- a `@All()` decorator function to set the metadata to define that a specific method in the `class` is used to catch all the requests coming to a particular endpoint.

### TL;DR

```ts
import { Controller, All } from "@nestjs/common";

// the @Controller() decorator function will instruct Nestjs
// to add a route of `/greet`
@Controller("greet")
export class GreetController {
  // the @All() decorator function will instruct Nestjs
  // that this is the default method that should be
  // invoked always when the user requests to
  // `/greet` endpoint with any methods
  @All()
  sayHello() {
    return `Hello World`;
  }
}
```

**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?](https://melvingeorge.me/blog/create-new-project-nestjs) if you have not made a Nestjs project.**

For example, let's say we need to make a simple request API endpoint called `/greet` and on request to this endpoint with any methods such as `GET`, `POST`, `PUT`, `DELETE` OR `PATCH` it should return a simple string response called `Hello World!`.

So to do this first let's make a file to write the request code to catch all the requests, 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,

```bash
mkdir greet && touch greet/greet.controller.ts
```

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

```bash
- greet
 - greet.controller.ts
```

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

It can be done like this,

```ts
export class GreetController {}
```

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

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

export class GreetController {}
```

Now let's use the `@Controller()` decorator function before the `GreetController` 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,

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

// makes the /greet route
@Controller("greet")
export class GreetController {}
```

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,

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

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

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

```ts
import { Controller, All } from "@nestjs/common";

// the @Controller() decorator function will instruct Nestjs
// to add a route of `/greet`
@Controller("greet")
export class GreetController {
  // the @All() decorator function will instruct Nestjs
  // that this is the default method that should be
  // invoked always when the user requests to
  // `/greet` endpoint with any methods
  @All()
  sayHello() {
    return `Hello World`;
  }
}
```

**NOTE: If you look at the `@All()` 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 `@Delete()` decorator function.**

The code for the `/greet` controller is done. The only thing left is to import our `GreetController` 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](https://melvingeorge.me/blog/create-new-project-nestjs)), there you need to import the `GreetController` class and add it as a controller in the `controllers` array in the `@Module()` decorator function.

It can be done like this,

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

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

@Module({
  imports: [],
  controllers: [GreetController], // <- add controller here
  providers: [],
})
export class AppModule {}
```

Now we have made a simple API endpoint called `/greet` that catches any requests of any methods successfully in the Nestjs project. Yay 🥳!

If you request to the `https://localhost:3000/greet` with any methods, you can see the response of `Hello World` from the server.

See the above code live in [codesandbox](https://codesandbox.io/s/make-simple-api-endpoint-catches-all-requests-with-any-methods-nestjs-w5crzw?file=/src/greet/greet.controller.ts).

Go to [Hoppscotch API tester 🚀](https://hopp.sh/r/R2ELojnjsMxJ) to send a request with any methods such as `GET`, `POST`, `PUT`, `DELETE`, `PATCH`, etc to the URL in the above codesandbox container.

**NOTE: If you were not able to send a request using the above Hoppscotch URL try turning on the `proxy` in the app.**

That's all 😃!

### Feel free to share if you found this useful 😃.

---