Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 2073

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T02:03:09+00:00 2024-11-26T02:03:09+00:00

Nest JS REST API Tutorial

  • 61k

Nest JS REST API Tutorial

Getting started

The first things you'll need are:

  • Node installed
  • Your favourite Code Editor/IDE

Once you have those setup, let us get started with nest.
Install the nest js CLI:

npm i -g @nestjs/cli 
Enter fullscreen mode Exit fullscreen mode

Now we can use this to bootstrap our nest project, using the following command:

nest new <project-name> 
Enter fullscreen mode Exit fullscreen mode

The command should start scaffolding your app, select your preferred package manager and let the CLI setup and install dependencies.

nest new.png

Then navigate into your project directory and start the development server.

cd nest-beginner npm run start:dev 
Enter fullscreen mode Exit fullscreen mode

Open a browser and go to http://localhost:3000/, and you should see a familiar message

nest start-dev output.png

Developing our API

To keep this tutorial simple, we are going to use nest CLI's resource generator recipe for generating our resources. Go ahead and run the following command to generate the User resource for our API.

nest generate resource users

For this tutorial, we will be developing a REST API, so go ahead and select that option and also let nest generate the CURD entry points to give us some boilerplate code to get started with.

After running the command successfully, you should have the following files in src/ directory:

nest resource gen file output.png

We start by defining the user entity and the DTOs:

// user.entity.ts export class User {   id: number;   username: string;   email: string;   password: string; } 
Enter fullscreen mode Exit fullscreen mode

// create-user.dto.ts export class CreateUserDto {   username: string;   email: string;   password: string; } 
Enter fullscreen mode Exit fullscreen mode

Since we are using nest generated resource's boilerplate, it makes it simpler as we need to modify just the business logic in the service layer which the controller is already utilising in the API layer. (for this tutorial, we will be storing everything in-memory, do note that in a real-world application we would use a database like MySQL or MongoDB)

// user.service.ts @Injectable() export class UsersService {   private users: User[] = [];   private idSeq = 0;    create(createUserDto: CreateUserDto) {   }    findAll(): User[] {     return this.users;   }    findOne(id: number): User {   }    update(id: number, updateUserDto: UpdateUserDto): User {   }    remove(id: number): User {   } } 
Enter fullscreen mode Exit fullscreen mode

Let us begin with the Get methods first.

// user.service.ts   findAll(): User[] {     return this.users;   }    findOne(id: number): User {     return this.users.find((user) => user.id === id);   } 
Enter fullscreen mode Exit fullscreen mode

To create the user, we push the create user DTO and use the idSeq variable to generate a sequential id for it:

// user.service.ts   create(createUserDto: CreateUserDto) {     this.users.push({       ...createUserDto,       id: this.idSeq++,     });     return this.users.at(-1);   } 
Enter fullscreen mode Exit fullscreen mode

To update the user, we first find the index by the id, if the user exists then we overwrite the values with the update user DTO.

// user.service.ts   update(id: number, updateUserDto: UpdateUserDto): User {     const i = this.users.findIndex((user) => user.id == id);     if (i === -1) return null;     this.users[i] = {       ...this.users[i],       ...updateUserDto,     };     return this.users[i];   } 
Enter fullscreen mode Exit fullscreen mode

For deleting, we similarly find if the user exists by id, and use the Array slice method to delete it from memory:

// user.service.ts   remove(id: number): User {     const i = this.users.findIndex((user) => user.id == id);     if (i === -1) return null;     const user = this.users[i];     this.users.splice(i, 1);     return user;   } 
Enter fullscreen mode Exit fullscreen mode

Now all of our CRUD functionalities are in place and we can test our API, yes you heard that right, we don't need to wire up the controller, set up the module and wire it up with our app, the nest CLI did all of that for us when we generated users resource. So fire up Postman or Insomnia or whatever is your favourite HTTP client.

You can find the source code for this article on this GitHub repo.

Feel free to reach out to me on Twitter @cryptus_neoxys and connect with me on LinkedIn.


Refs & Resources

Nest JS Docs

beginnersnodetypescriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.