Set Up GET Queries - Get All and Get By ID
Introduction and Context Setting
Welcome to the first lesson of our course on "Building a To-Do REST API with NestJS". In this course, we'll learn how to create a To-Do application using NestJS. A REST API (Representational State Transfer Application Programming Interface) is a way for different software systems to communicate over the internet. NestJS is a framework for building efficient, reliable, and scalable server-side applications with REST at the core.
NestJS provides built-in support for complete application development, making it a versatile choice for building APIs. It is built on top of Express.js but adds many valuable features and an Angular-inspired structure to make development easier.
Setting Up the Project Environment
Before we dive into the code, let's set up our project environment. Although the CodeSignal environment comes pre-installed with necessary libraries, it's essential to know how to set things up locally.
-
Installing NestJS Locally:
- First, you need
Node.jsinstalled on your system. You can download it from Node.js. - Install the Nest CLI globally using
npm: - Create a new project using the Nest CLI:
- First, you need
-
Project Structure:
- The basic file structure includes:
src/folder: Contains the main application files.main.ts: The entry file for the application.app.module.ts: The root module of the application.
- The basic file structure includes:
For our example, we'll assume the environment is already set up on CodeSignal.
Creating the TodoController
Controllers in NestJS handle incoming requests and return responses to the client. Let's create our TodoController. In a REST API, you will want to map web requets with different verbs. The most common verb is GET which indicates that you are querying data, not modifying it. It is very common in a REST API to include two GET handlers for a Resource. In this case, our Resource is Todo and our GET queries handle getting all Todos in addition to getting a single Todo by ID.
Step-by-Step Code
