This project is a simple REST API built using Node.js, Express, and MongoDB. It supports basic CRUD (Create, Read, Update, Delete) operations for user management.
As a frontend developer, I created this project to gain a deeper understanding of how REST APIs function.
Make sure you have the following installed before starting:
- Node.js
- MongoDB - You can create a free MongoDB Atlas account here.
git clone https://github.com/azlibdar/rest-api.git
cd rest-api
npm install
-
Create a
.env
file in the project root -
Copy contents from
.env.example
-
Fill in your specific configuration:
MONGO_URI=<Your MongoDB URI> # Connection string for your MongoDB database. CORS_ORIGIN=http://localhost:8080 # Allowed origin for Cross-Origin Resource Sharing (CORS). AUTH_SECRET=<Your Auth Secret> # Used for authentication and password hashing. Defaults to `AZLAN-REST-API` if not provided.
npm start
The server will be running on
http://localhost:8080
.
Route (POST): /auth/register
Request body: { "username": "abc", "email": "abc@example.com", "password": "abc123" }
Route (POST): /auth/login
Request body: { "email": "abc@example.com", "password": "abc123" }
Response: A session token will be generated.
Route (POST): /auth/logout
Request body: N/A
Response: The user's session token will be invalidated.
Route (GET): /list-users
Request body: N/A
Authentication required.
Route (DELETE): /delete-account/:id
Request body: N/A
Authentication required. User must be the owner of the account.
Route (PATCH): /change-password/:id
Request body: { "oldPassword": "123", "newPassword": "1234" }
Response: The user's session token will be invalidated, and a new salt will be generated.
Authentication required. User must be the owner of the account.
Contributions are welcome! Feel free to suggest features, report issues, or submit pull requests.