Skip to content

CorentinDotDev/API-Template

Repository files navigation

Template for API

This is a Temlate for building an API, a backend service designed to support basic functionality for an application. It provides endpoints for authentication, profile management, post interactions, and comments.

Prerequisites

Before you begin, ensure you have met the following requirements:

  • You have installed Node.js (version 14 or above recommended).
  • You have installed Docker and Docker Compose if you wish to run the database in a Docker container.
  • You have a PostgreSQL database setup either locally or remotely.

Installation

Follow these steps to get your development environment set up:

  1. Clone the repository to your local machine:
git clone https://github.com/your/repository.git
cd repository
  1. Install the necessary npm packages:
npm install
  1. Copy the .env.example file to .env and fill in the necessary environment variables.
cp .env.example .env
  1. Run the database migrations:
npx prisma migrate deploy
  1. (Optional) if you are running the database in a Docker container, start the database:
docker-compose up -d
  1. Start the development server:
npm run dev

API should now be running at http://localhost:3000 or the port specified in the .env file.

You can access the API documentation at http://localhost:3000/api-docs.

Usage

To test the API, you can send requests to the various endpoints using the swagger documentation or a tool like Postman.

Example requests :

  1. User registration
fetch("http://localhost:3000/auth/register", {
  method: "POST",
  headers: {
    accept: "application/json",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    username: "YOUR_USERNAME",
    email: "YOUR@EMAIL.COM",
    password: "YOUR_PASSWORD",
  }),
})
  .then((response) => response.json())
  .then((data) => console.log(data))
  .catch((error) => console.error(error));
  1. Use login endpoint to get a token
fetch("http://localhost:3000/auth/login", {
  method: "POST",
  headers: {
    accept: "application/json",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    email: "YOUR@EMAIL.COM",
    password: "YOUR_PASSWORD",
  }),
})
  .then((response) => response.json())
  .then((data) => console.log(data))
  .catch((error) => console.error(error));
  1. Use the token to authenticate use authenticated endpoints
fetch("http://localhost:3000/endPointExample", {
  method: "GET",
  headers: {
    accept: "application/json",
    Authorization: "Bearer YOUR_TOKEN",
  },
})
  .then((response) => response.json())
  .then((data) => console.log(data))
  .catch((error) => console.error(error));

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors