To make a simple PUT
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
PUT
request method. - a
@Controller()
decorator function before the controller class to define the metadata for the route. - a
@Put()
decorator function to set the metadata to define that a specific method in theclass
is used for a particularPUT
request.
TL;DR
import { Controller, Put } from "@nestjs/common";
// the @Controller() decorator function will instruct Nestjs
// to add a route of `/greet`
@Controller("greet")
export class GreetController {
// the @Put() decorator function will instruct Nestjs
// that this is the default method that should be
// invoked when the user requests a `PUT` to `/greet` endpoint
@Put()
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? if you have not made a Nestjs project.
For example, let's say we need to make a simple PUT
request API endpoint called /greet
and on requesting to this endpoint it should return a simple string response called Hello World!
.
So to do this first let's make a file to write the PUT
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
After running the above command, you will have a structure that looks something like this,
- 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,
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,
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,
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,
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 the request method is a PUT
and the route is /greet
we need to run the sayHello
method, to do that let's use the @Put()
decorator function from the @nestjs/common
module and use it before the sayHello
method without any arguments like this,
import { Controller, Put } from "@nestjs/common";
// the @Controller() decorator function will instruct Nestjs
// to add a route of `/greet`
@Controller("greet")
export class GreetController {
// the @Put() decorator function will instruct Nestjs
// that this is the default method that should be
// invoked when the user requests a `PUT` to `/greet` endpoint
@Put()
sayHello() {
return `Hello World`;
}
}
NOTE: If you look at the @Put()
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 @Put()
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), 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,
/* 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 PUT
method /greet
endpoint successfully in the Nestjs project. Yay 🥳!
If you make a PUT
request to the https://localhost:3000/greet
, you can see the response of Hello World
from the server.
See the above code live in codesandbox.
Go to Hoppscotch API tester 🚀 to send a PUT
request 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 😃!