Skip to content

Ethereal-Services/example-rest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Express REST API

A Node.js Express application with 6 REST API endpoints demonstrating both GET and POST methods.

Features

  • 3 GET endpoints for data retrieval
  • 3 POST endpoints for data creation and authentication
  • JSON request/response handling
  • Basic validation and error handling
  • Mock data storage (in-memory)
  • Health check endpoint

Installation

Option 1: Local Development

  1. Install dependencies:
npm install
  1. Start the server:
npm start

Or for development with auto-restart:

npm run dev

The server will start on port 3000 (or the port specified in the PORT environment variable).

Option 2: Docker

Using Docker directly:

  1. Build the Docker image:
docker build -t express-rest-api .
  1. Run the container:
docker run -p 3000:3000 --name express-app express-rest-api

Using Docker Compose (Recommended):

  1. Build and start the application:
docker-compose up --build
  1. Run in background (detached mode):
docker-compose up -d --build
  1. Stop the application:
docker-compose down

The Docker setup includes:

  • Express API running on port 3000
  • Nginx reverse proxy (optional) on port 80
  • Health checks for container monitoring
  • Security: Non-root user execution

API Endpoints

GET Endpoints

1. Health Check

  • URL: /health
  • Method: GET
  • Description: Check if the server is running
  • Response:
{
  "status": "OK",
  "message": "Server is running",
  "timestamp": "2023-10-01T12:00:00.000Z",
  "uptime": 42.5
}

2. Get All Users

  • URL: /users
  • Method: GET
  • Description: Retrieve all users with optional pagination
  • Query Parameters:
    • limit (optional): Number of users to return
    • offset (optional): Number of users to skip
  • Example: /users?limit=2&offset=1
  • Response:
{
  "users": [
    {
      "id": 1,
      "name": "John Doe",
      "email": "john@example.com",
      "age": 30
    }
  ],
  "total": 3,
  "count": 1
}

3. Get User by ID

  • URL: /users/:id
  • Method: GET
  • Description: Retrieve a specific user by their ID
  • Example: /users/1
  • Response:
{
  "id": 1,
  "name": "John Doe",
  "email": "john@example.com",
  "age": 30
}

POST Endpoints

1. Create User

  • URL: /users
  • Method: POST
  • Description: Create a new user
  • Request Body:
{
  "name": "Alice Brown",
  "email": "alice@example.com",
  "age": 28
}
  • Response:
{
  "message": "User created successfully",
  "user": {
    "id": 4,
    "name": "Alice Brown",
    "email": "alice@example.com",
    "age": 28
  }
}

2. User Login

  • URL: /login
  • Method: POST
  • Description: Authenticate a user (demo - any password works for existing users)
  • Request Body:
{
  "email": "john@example.com",
  "password": "any-password"
}
  • Response:
{
  "message": "Login successful",
  "user": {
    "id": 1,
    "name": "John Doe",
    "email": "john@example.com"
  },
  "token": "fake-jwt-token-1"
}

3. Submit Feedback

  • URL: /feedback
  • Method: POST
  • Description: Submit user feedback
  • Request Body:
{
  "subject": "Great app!",
  "message": "I love using this application.",
  "rating": 5,
  "userEmail": "user@example.com"
}
  • Response:
{
  "message": "Feedback submitted successfully",
  "feedback": {
    "id": 1696176000000,
    "subject": "Great app!",
    "timestamp": "2023-10-01T12:00:00.000Z"
  }
}

Testing the API

You can test the API using curl commands:

GET Examples:

# Health check
curl http://localhost:3000/health

# Get all users
curl http://localhost:3000/users

# Get specific user
curl http://localhost:3000/users/1

# Get users with pagination
curl "http://localhost:3000/users?limit=2&offset=1"

POST Examples:

# Create a new user
curl -X POST http://localhost:3000/users \
  -H "Content-Type: application/json" \
  -d '{"name":"Alice Brown","email":"alice@example.com","age":28}'

# Login
curl -X POST http://localhost:3000/login \
  -H "Content-Type: application/json" \
  -d '{"email":"john@example.com","password":"any-password"}'

# Submit feedback
curl -X POST http://localhost:3000/feedback \
  -H "Content-Type: application/json" \
  -d '{"subject":"Great app!","message":"I love this application.","rating":5,"userEmail":"user@example.com"}'

Error Responses

The API returns appropriate HTTP status codes and error messages:

  • 400 Bad Request: Invalid request data
  • 401 Unauthorized: Authentication failed
  • 404 Not Found: Resource not found
  • 409 Conflict: Resource already exists
  • 500 Internal Server Error: Server error

Example error response:

{
  "error": "Validation failed",
  "message": "Name and email are required fields"
}

Default Data

The application starts with 3 sample users:

Notes

  • This is a demo application using in-memory storage
  • Data will be lost when the server restarts
  • The login endpoint accepts any password for existing users (for demo purposes)
  • In a production environment, you would use a real database and proper authentication

Docker Commands

Useful Docker commands for this application:

# Build the image
docker build -t express-rest-api .

# Run container
docker run -p 3000:3000 --name express-app express-rest-api

# Run container in background
docker run -d -p 3000:3000 --name express-app express-rest-api

# View container logs
docker logs express-app

# Stop container
docker stop express-app

# Remove container
docker rm express-app

# Remove image
docker rmi express-rest-api

# Using Docker Compose
docker-compose up --build          # Build and run
docker-compose up -d --build       # Build and run in background
docker-compose logs                # View logs
docker-compose down                # Stop and remove containers
docker-compose down --volumes      # Stop and remove containers + volumes

Docker Health Check

The container includes a health check that monitors the /health endpoint:

  • Interval: Every 30 seconds
  • Timeout: 3 seconds
  • Retries: 3 attempts
  • Start Period: 5 seconds

Check container health status:

docker inspect --format='{{.State.Health.Status}}' express-app

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published