- Create a simple Express.js API that connects to a PostgreSQL database and performs basic database operations (CRUD - Create, Read, Update, Delete).
- Node.js installed
- PostgreSQL installed and running
- Clone the repository.
- Create database:
CREATE DATABASE your_db_name; \c your_db_name \i init_db.sql
- Install dependencies:
npm install express pg
- Configure environment: Create a
.envfile or set env vars:PG_USER=your_db_user PG_PASSWORD=your_db_password PG_DATABASE=your_db_name PG_HOST=localhost PG_PORT=5432
- Run server:
node server.js
| Method | Endpoint | Description |
|---|---|---|
| GET | /users |
Retrieve all users |
| GET | /users/:id |
Retrieve user by ID |
| POST | /users |
Create a new user |
| PUT | /users/:id |
Update a user by ID |
| DELETE | /users/:id |
Delete a user by ID |
# Create user
curl -X POST http://localhost:3000/users \
-H 'Content-Type: application/json' \
-d '{"name":"Alice","email":"alice@example.com","age":30}'
# Get all users
curl http://localhost:3000/users