This project is a simple RESTful API built with Node.js, TypeScript, and MongoDB as the database. It provides endpoints to create and manage products in a store.
- Features
- Prerequisites
- Installation
- Usage
- Endpoints
- Authentication
- Testing
- Docker
- Contributing
- License
- Create, read, and manage products in a store.
- Token-based authentication for secure endpoints.
- TypeScript for enhanced code quality and maintainability.
- MongoDB for efficient data storage.
Before you begin, ensure you have met the following requirements:
- Node.js and npm installed.
- MongoDB server running locally or providing a connection URI.
-
Clone the repository:
git clone https://github.com/Davidosky007/backend-assesment-api.git
-
Install dependencies:
npm install
-
Create a
.env
file in the root directory and add the following environment variables:
PORT=8080
MONGO_URI= a hosted MongoDB URL
JWT_SECRET=secret
-
Start the server:
npm start
-
Navigate to
http://localhost:8080/
to access the API.
Method | Endpoint | Description | Authentication |
---|---|---|---|
POST | /api/users/register |
Register a new user | No |
POST | /api/users/login |
Login with an existing user | No |
GET | api/products |
Get all products | Yes |
GET | api/products/:id |
Get a single product by ID | Yes |
POST | api/products |
Create a new product | Yes |
PUT | api/products/:id |
Update an existing product by ID | Yes |
DELETE | api/products/:id |
Delete an existing product by ID | Yes |
Some endpoints require authentication. To authenticate a request, add the Authorization
header with the value Bearer <token>
, where <token>
is the access token returned from the login endpoint.
Create a .env file in the root directory and add the following environment variables:
NODE_ENV=DEV
MONGO_URI_DEV=<A hosted mongodb database>
JWT_SECRET=<secret>
To run the tests, run the following command:
npm run test
To run the app in a docker container, run the following command:
docker build -t project_name.
docker run -d -p 8080:8080 project_name