Tutorial NestJS RESTful API (Bahasa Indonesia)
Table of Contents
Introduction
This tutorial will guide you through building a RESTful API using NestJS, a progressive Node.js framework. We will cover user, contact, and address management functionalities, setup requirements, and how to implement various API endpoints. This tutorial is particularly useful for developers looking to enhance their backend skills with NestJS.
Step 1: Set Up Requirements
Before you start building your API, ensure you have the following prerequisites:
- Node.js installed on your machine.
- A code editor (like Visual Studio Code).
- Basic understanding of JavaScript and TypeScript.
Step 2: Create a New NestJS Project
To create a new NestJS project, follow these steps:
- Open your terminal.
- Run the following command to install the NestJS CLI globally:
npm install -g @nestjs/cli
- Create a new project:
nest new my-nestjs-api
- Navigate to your project directory:
cd my-nestjs-api
Step 3: Define API Specifications
Define the specifications for the User, Contact, and Address APIs:
- User API
- Register User
- Login User
- Get User
- Update User
- Logout User
- Contact API
- Create Contact
- Get Contact
- Update Contact
- Remove Contact
- Search Contact
- Address API
- Create Address
- Get Address
- Update Address
- Remove Address
- List Address
Step 4: Set Up Database
To set up your database:
- Choose a database (e.g., PostgreSQL, MongoDB).
- Install the required database driver:
npm install --save @nestjs/typeorm typeorm pg
- Configure the database connection in the
app.module.ts
file.
Step 5: Create Models
Create models for User, Contact, and Address:
- User Model
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; @Entity() export class User { @PrimaryGeneratedColumn() id: number; @Column() username: string; @Column() password: string; }
- Contact Model
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; @Entity() export class Contact { @PrimaryGeneratedColumn() id: number; @Column() name: string; @Column() email: string; }
- Address Model
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; @Entity() export class Address { @PrimaryGeneratedColumn() id: number; @Column() street: string; @Column() city: string; }
Step 6: Implement API Endpoints
Implement each API endpoint as follows:
User API
-
Register User
- Create a service and controller for user registration.
- Use POST method to handle registration.
-
Login User
- Implement authentication logic.
-
Get User
- Create a GET method to retrieve user details.
-
Update User
- Use PUT method to update user information.
-
Logout User
- Implement logout functionality.
Contact API
-
Create Contact
- Implement a POST method to add a new contact.
-
Get Contact
- Use GET method to retrieve contact details.
-
Update Contact
- Use PUT method for updating contact information.
-
Remove Contact
- Implement DELETE method to remove a contact.
-
Search Contact
- Implement search functionality with query parameters.
Address API
-
Create Address
- Implement a POST method for adding addresses.
-
Get Address
- Use GET method to retrieve address details.
-
Update Address
- Use PUT method for updating address information.
-
Remove Address
- Implement DELETE method to remove an address.
-
List Address
- Create a method to list all addresses.
Step 7: Test Your API
Once you have implemented all the endpoints, it's crucial to test your API:
- Use tools like Postman or Insomnia to send requests to your endpoints.
- Verify that each API responds correctly and handles errors gracefully.
Conclusion
In this tutorial, we covered how to create a RESTful API using NestJS, including setting up your project, defining API specifications, creating models, and implementing endpoints. Next steps involve deploying your API to a cloud service and exploring more advanced features of NestJS such as middleware and guards to enhance your application. Happy coding!