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.
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.
Follow these steps to get your development environment set up:
- Clone the repository to your local machine:
git clone https://github.com/your/repository.git
cd repository- Install the necessary npm packages:
npm install- Copy the
.env.examplefile to.envand fill in the necessary environment variables.
cp .env.example .env- Run the database migrations:
npx prisma migrate deploy- (Optional) if you are running the database in a Docker container, start the database:
docker-compose up -d- Start the development server:
npm run devAPI 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.
To test the API, you can send requests to the various endpoints using the swagger documentation or a tool like Postman.
Example requests :
- 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));- 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));- 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));