- Published on
How to Create a Simple Note Application API Using Nest.js
- Authors
- Name
- Faruk Kaledibi
Introduction
Nest.js is a powerful and extensible Node.js framework that allows developers to build scalable and maintainable server-side applications. In this tutorial, we will guide you through the process of creating a simple note application API using Nest.js. We'll cover the basics of setting up a Nest.js project, defining routes, and interacting with a database.
Prerequisites
Before we begin, make sure you have Node.js and npm installed on your machine. You can install Nest.js using the following command:
npm install -g @nestjs/cli
Setting Up Your Nest.js Project
Let's start by creating a new Nest.js project. Open your terminal and run the following commands:
nest new simple-note-api
cd simple-note-api
This will create a new Nest.js project and navigate into its directory.
Creating the Note Module
In Nest.js, functionality is organized into modules. Let's create a module for our notes. Run the following command to generate a new module:
nest generate module notes
This will create a notes
directory with the necessary files.
Now, let's generate a controller for our notes module:
nest generate controller notes
Defining Routes
Open the notes.controller.ts
file in the src/notes
directory. Define the routes for creating, retrieving, updating, and deleting notes:
// src/notes/notes.controller.ts
import { Controller, Get, Post, Put, Delete, Body, Param } from '@nestjs/common';
import { CreateNoteDto, UpdateNoteDto } from './dto';
@Controller('notes')
export class NotesController {
@Get()
getAllNotes() {
// Retrieve all notes logic
}
@Get(':id')
getNoteById(@Param('id') id: string) {
// Retrieve a note by ID logic
}
@Post()
createNote(@Body() createNoteDto: CreateNoteDto) {
// Create a new note logic
}
@Put(':id')
updateNote(@Param('id') id: string, @Body() updateNoteDto: UpdateNoteDto) {
// Update a note by ID logic
}
@Delete(':id')
deleteNote(@Param('id') id: string) {
// Delete a note by ID logic
}
}
Creating Data Transfer Objects (DTOs)
DTOs are used to define the structure of data passed between the client and the server. Create dto.ts
file in the src/notes
directory:
// src/notes/dto.ts
export class CreateNoteDto {
readonly title: string;
readonly content: string;
}
export class UpdateNoteDto {
readonly title?: string;
readonly content?: string;
}
Interacting with a Database
For simplicity, we'll use a mock database using an array. In a real-world scenario, you would connect Nest.js to a database like MongoDB or PostgreSQL.
In the notes.service.ts
file in the src/notes
directory, implement the logic to interact with the database:
// src/notes/notes.service.ts
import { Injectable } from '@nestjs/common';
import { CreateNoteDto, UpdateNoteDto } from './dto';
@Injectable()
export class NotesService {
private notes = [];
getAllNotes() {
return this.notes;
}
getNoteById(id: string) {
return this.notes.find(note => note.id === id);
}
createNote(createNoteDto: CreateNoteDto) {
const newNote = {
id: Math.random().toString(36).substring(7),
...createNoteDto,
};
this.notes.push(newNote);
return newNote;
}
updateNote(id: string, updateNoteDto: UpdateNoteDto) {
const index = this.notes.findIndex(note => note.id === id);
if (index !== -1) {
this.notes[index] = { ...this.notes[index], ...updateNoteDto };
return this.notes[index];
}
return null;
}
deleteNote(id: string) {
this.notes = this.notes.filter(note => note.id !== id);
}
}
Wrapping Up
Congratulations! You've created a simple note application API using Nest.js. This tutorial covered the basics of setting up a Nest.js project, defining routes, creating data transfer objects, and interacting with a mock database. You can further enhance this application by connecting it to a real database and adding authentication.
Feel free to explore more features of Nest.js and customize the application according to your needs. Happy coding!