A Node.js Express application with 6 REST API endpoints demonstrating both GET and POST methods.
- 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
- Install dependencies:
npm install
- 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).
- Build the Docker image:
docker build -t express-rest-api .
- Run the container:
docker run -p 3000:3000 --name express-app express-rest-api
- Build and start the application:
docker-compose up --build
- Run in background (detached mode):
docker-compose up -d --build
- 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
- 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
}
- URL:
/users
- Method:
GET
- Description: Retrieve all users with optional pagination
- Query Parameters:
limit
(optional): Number of users to returnoffset
(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
}
- 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
}
- 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
}
}
- 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"
}
- 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"
}
}
You can test the API using curl commands:
# 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"
# 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"}'
The API returns appropriate HTTP status codes and error messages:
400 Bad Request
: Invalid request data401 Unauthorized
: Authentication failed404 Not Found
: Resource not found409 Conflict
: Resource already exists500 Internal Server Error
: Server error
Example error response:
{
"error": "Validation failed",
"message": "Name and email are required fields"
}
The application starts with 3 sample users:
- John Doe (john@example.com)
- Jane Smith (jane@example.com)
- Bob Johnson (bob@example.com)
- 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
# 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
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