Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

API Application

A RESTful API built with Express.js, PostgreSQL, and Valkey for caching.

Features

  • User authentication with JWT
  • Password encryption with bcrypt
  • PostgreSQL database for data storage
  • Valkey (Redis-compatible) for caching and session management
  • RESTful API endpoints

Prerequisites

  • Node.js (v14 or higher)
  • Docker and Docker Compose

Getting Started

  1. Clone the repository
  2. Install dependencies:
    npm install
    
  3. Start the services (PostgreSQL and Valkey):
    docker-compose up -d
    
  4. Start the application:
    npm run dev
    

Environment Variables

The application uses the following environment variables (already set in the .env file):

  • DB_HOST: PostgreSQL host
  • DB_PORT: PostgreSQL port
  • DB_NAME: PostgreSQL database name
  • DB_USER: PostgreSQL username
  • DB_PASSWORD: PostgreSQL password
  • VALKEY_HOST: Valkey host
  • VALKEY_PORT: Valkey port
  • JWT_SECRET: Secret key for JWT
  • JWT_EXPIRES_IN: JWT expiration time
  • PORT: Server port

API Endpoints

Authentication

  • 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"
        }
      }
  • 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"
        }
      }
  • Logout

    • URL: /api/auth/logout
    • Method: POST
    • Headers:
      Authorization: Bearer jwt_token
      
    • Response:
      {
        "success": true,
        "message": "Logout successful"
      }
  • 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"
        }
      }

Testing the API

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"

Architecture

  • config/: Configuration files for database, Valkey, and Passport
  • models/: Database models
  • routes/: API routes
  • middleware/: Custom middleware
  • utils/: Utility functions

Caching Strategy

  • 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

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages