A RESTful API built with Express.js, PostgreSQL, and Valkey for caching.
- User authentication with JWT
- Password encryption with bcrypt
- PostgreSQL database for data storage
- Valkey (Redis-compatible) for caching and session management
- RESTful API endpoints
- Node.js (v14 or higher)
- Docker and Docker Compose
- Clone the repository
- Install dependencies:
npm install - Start the services (PostgreSQL and Valkey):
docker-compose up -d - Start the application:
npm run dev
The application uses the following environment variables (already set in the .env file):
DB_HOST: PostgreSQL hostDB_PORT: PostgreSQL portDB_NAME: PostgreSQL database nameDB_USER: PostgreSQL usernameDB_PASSWORD: PostgreSQL passwordVALKEY_HOST: Valkey hostVALKEY_PORT: Valkey portJWT_SECRET: Secret key for JWTJWT_EXPIRES_IN: JWT expiration timePORT: Server port
-
Register a new user
- URL:
/api/auth/register - Method:
POST - Body:
{ "username": "example", "email": "example@example.com", "password": "password123" } - Response:
{ "success": true, "message": "User registered successfully", "token": "jwt_token", "user": { "id": 1, "username": "example", "email": "example@example.com" } }
- URL:
-
Login
- URL:
/api/auth/login - Method:
POST - Body:
{ "username": "example", "password": "password123" } - Response:
{ "success": true, "message": "Login successful", "token": "jwt_token", "user": { "id": 1, "username": "example", "email": "example@example.com" } }
- URL:
-
Logout
- URL:
/api/auth/logout - Method:
POST - Headers:
Authorization: Bearer jwt_token - Response:
{ "success": true, "message": "Logout successful" }
- URL:
-
Get Current User
- URL:
/api/auth/me - Method:
GET - Headers:
Authorization: Bearer jwt_token - Response:
{ "success": true, "user": { "id": 1, "username": "example", "email": "example@example.com" } }
- URL:
You can use tools like Postman or curl to test the API endpoints.
Example with curl:
# Register a new user
curl -X POST http://localhost:8080/api/auth/register \
-H "Content-Type: application/json" \
-d '{"username":"example","email":"example@example.com","password":"password123"}'
# Login
curl -X POST http://localhost:8080/api/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"example","password":"password123"}'
# Get current user (replace jwt_token with the token from login response)
curl -X GET http://localhost:8080/api/auth/me \
-H "Authorization: Bearer jwt_token"
# Logout (replace jwt_token with the token from login response)
curl -X POST http://localhost:8080/api/auth/logout \
-H "Authorization: Bearer jwt_token"- config/: Configuration files for database, Valkey, and Passport
- models/: Database models
- routes/: API routes
- middleware/: Custom middleware
- utils/: Utility functions
- User data is cached in Valkey for 1 hour
- Session information is stored in Valkey for 24 hours
- GET requests are cached for 30 minutes
- If data is not in the cache, it is retrieved from the database