This repository contains a REST (Representational State Transfer) API used to manage the database in a university context.
This API is just an example on how does a REST API works, you can clone this repository to use it as a blueprint if you are trying to create a RESTful API:
git clone https://github.com/JS-Bej/REST-Blueprint.git
cd REST-Blueprint
APIs (Application Programming Interfaces) are used to enable communication between machines or software.
You can find them both on websites and in applications. For example:
The Google Maps API allows us to use Google Maps on our website or application.
There are several types of APIs, such as REST, SOAP, Web Socket, Webhook, etc.... However, we will focus on REST, which is commonly used in web applications with microservices, such as e-commerce systems. This type of API is characterized by using only HTTP methods:
GET
: To retrieve resources.POST
: To create new resources.PUT
: To update existing resources.DELETE
: To delete resources.
This API was created with TypeScript and frameworks such as Node.js, Express.js, etc. Remember that the
dist
directory contains the JS files that TypeScript transpiled so our browser can use them.
In this directory you will find:
models
: Defines the proper structure of each entity so that a resource can be correctly stored in the database.controllers
: Defines the possible actions for each entity (Remember that, as a REST API, we will only use HTTP methods).routes
: Defines the routes to access, add, update, and delete resources for each entity.
This file contains the credentials that will be used for the database connection.
With db.ts
, we can create the connection to the database by providing the necessary credentials for a MySQL database. For example:
Using host: process.env.DB_HOST
, we indicate that the database host is found in a DB_HOST
variable in the .env
file.
NOTE
The.env
file is included in the repository so you can use this project as a guide. However, in a professional setting, you should NOT include this file since it contains the database credentials.
It is important to initialize the database and connect it to the application before making any requests. In the package.json
file, you will find scripts that simplify this process.
First of all, make sure you have all the necessary dependencies installed. You can do this by running:
npm install @types/cors body-parser cors dotenv express mysql2 typescript
npm install --save-dev @types/body-parser @types/dotenv @types/express @types/mysql @types/node nodemon ts-node
To establish the connection between database and application, run the following command in the shell and do not close it:
npm run dev
Now you're all set to make requests. You can use tools like Postman or Insomnia to interact with your API endpoints. 🌐🔍
⭐ If you liked this project or just want to save it for later, feel free to star this repository ⭐