A RESTful API built with Node.js, Express, and PostgreSQL for user management. This application uses Docker for containerization and environment management.
- Features
- Project Structure
- Prerequisites
- Getting Started
- API Endpoints
- Error Handling
- Input Validation
- Database Schema
- Contributing
- License
- CRUD Operations: Create, read, update, and delete user records
- Input Validation: Request data validation using Joi
- Error Handling: Centralized error handling middleware
- PostgreSQL Database: Reliable relational database
- Docker Integration: Easy deployment with Docker containers
- Environment Configuration: Using dotenv for environment variables
project-root/
├── config/
│ └── db.js # Database connection configuration
├── controllers/
│ └── userController.js # Request handlers for user routes
├── data/
│ └── createUserTable.js # Database table initialization
├── middlewares/
│ ├── errorhandler.js # Error handling middleware
│ └── inputValidator.js # Request validation middleware
├── models/
│ └── userModel.js # Database service methods
├── routes/
│ └── userRoutes.js # API route definitions
├── .env # Environment variables (not tracked by git)
├── .gitignore # Git ignore file
├── server.js # Application entry point
├── Dockerfile # Docker configuration
├── docker-compose.yml # Docker Compose configuration
└── README.md # Project documentation
- Node.js (v14 or higher)
- Docker and Docker Compose
- Git
Create a .env file in the root directory with the following variables:
DB_USER=postgres
DB_HOST=postgres
DB_NAME=users_db
DB_PASSWORD=your_secure_password
DB_PORT=5432
- Clone the repository
git clone https://github.com/yourusername/postgres-node-api.git
cd postgres-node-api- Create and start containers
docker-compose up -dThis will:
- Create and start the PostgreSQL container
- Create and start the Node.js application container
- Set up the network between them
- Initialize the database with the users table
- Access the API
The API will be available at http://localhost:5001
- Clone the repository
git clone https://github.com/yourusername/postgres-node-api.git
cd postgres-node-api- Install dependencies
npm install- Set up PostgreSQL
Ensure PostgreSQL is installed and running. Create a database and update the .env file with your connection details.
- Start the application
npm start| Method | Endpoint | Description | Request Body | Response |
|---|---|---|---|---|
| POST | /api/user | Create a new user | { "name": "", "email": "" } |
User object |
| GET | /api/user | Get all users | None | Array of user objects |
| GET | /api/user/:id | Get user by ID | None | User object |
| PUT | /api/user/:id | Update a user | { "name": "", "email": "" } |
Updated user object |
| DELETE | /api/user/:id | Delete a user | None | Deleted user object |
curl -X POST http://localhost:5001/api/user \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "email": "john@example.com"}'{
"status": 201,
"message": "User created successfully",
"data": {
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"created_at": "2025-04-17T08:30:00.000Z"
}
}The API uses a centralized error handling middleware that returns consistent error responses:
{
"status": 500,
"message": "Something went wrong",
"error": "Error message details"
}Common HTTP status codes:
200: Success201: Resource created400: Bad request (validation error)404: Resource not found500: Server error
Request validation is handled by Joi. User inputs are validated against the following schema:
const userSchema = Joi.object({
name: Joi.string().min(3).max(30).required(),
email: Joi.string().email().required(),
})Users Table
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.