A simple API project using Express.js and PostgreSQL for managing users data.
- ✅ CRUD (Create, Read, Update, Delete) Users: Provides basic operations for managing user data.
- ✅ PostgreSQL as Database: Uses the reliable and powerful PostgreSQL relational database.
- ✅ Security Middleware:
- CORS (Cross-Origin Resource Sharing): Secures the API by controlling which domains are allowed to access resources.
- dotenv: Manages environment configurations such as database connections securely.
- Node.js: JavaScript runtime environment for running the server.
- Express.js: A minimal and flexible web framework for Node.js.
- PostgreSQL: An open-source relational database management system.
- dotenv: A Node.js module for loading environment variables from a
.envfile. - CORS: An Express.js middleware to enable cross-origin resource sharing.
Follow the steps below to install and run the User Management API project:
git clone https://github.com/USERNAME/user-management-api.git
cd user-management-apinpm installThis command will install all necessary packages and libraries listed in the package.json file.
Create a .env file in the root directory of your project (next to package.json) and add the following configuration:
PORT=5000
DATABASE_URL=postgresql://user:password@host:port/databaseExplanation:
PORT: Specifies the port where the server will run (default: 5000). You can change it as needed.DATABASE_URL: Your PostgreSQL connection string. Replaceuser,password,host,port, anddatabasewith your actual PostgreSQL credentials.
npm startThis command will start your Node.js server. If successful, you will see a message indicating that the server is running at the specified address (usually http://localhost:5000).
Below is a list of available API endpoints for user management:
| Method | Endpoint | Description |
|---|---|---|
| GET | /users |
Retrieves a list of all users. |
| GET | /users/:id |
Retrieves user details by ID. |
| POST | /users |
Adds a new user. |
| PUT | /users/:id |
Updates user data by ID. |
| DELETE | /users/:id |
Deletes a user by ID. |
Note:
:idin the endpoint represents a parameter that should be replaced with a specific user ID.
Below is the directory structure of the project:
/backend-api
├── /routes
│ └── userRoutes.js # Defines user routes
├── /controllers
│ └── userController.js # Handles business logic for user operations
├── /models
│ └── userModel.js # Interacts with the database for user data
├── index.js # Main file to run the Express server
├── .env # Contains environment configurations (do not commit)
├── package.json # Contains project information and dependencies
├── package-lock.json # Records exact versions of dependencies
├── README.md # Project documentation (this file)
You can use Postman, cURL, or any HTTP client application to test the API. Below are some examples using cURL:
curl -X GET http://localhost:5000/userscurl -X GET http://localhost:5000/users/1curl -X POST http://localhost:5000/users \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "email": "johndoe@example.com"}'curl -X PUT http://localhost:5000/users/1 \
-H "Content-Type: application/json" \
-d '{"name": "John Updated", "email": "johnupdated@example.com"}'curl -X DELETE http://localhost:5000/users/1Thank You!