A robust RESTful API built with Express.js and PostgreSQL, providing CRUD operations for managing blog posts.
- CRUD Operations: Create, Read, Update, and Delete blog posts
- PostgreSQL Integration: Robust database management using node-postgres
- Environment Configuration: Secure configuration using dotenv
- Error Handling: Comprehensive error management and logging
- Scalable Architecture: Designed for easy scaling and maintenance
- Node.js
- npm
- PostgreSQL
- Git
-
Clone the repository:
git clone https://github.com/avi-poptani-003/restful-blog-api-node-postgres.git
-
Install dependencies:
npm install
OR
npm i
-
Create a
.envfile in the root directory with the following content:PORT=your PORT DB_USER=database username DB_HOST=localhost DB_NAME=Database name (ex:my-DB) DB_PASSWORD=your database password DB_PORT=your database port -
Adjust the values according to your environment.
-
Create the database:
CREATE DATABASE "db-name"; -
Create a
poststable :CREATE TABLE posts ( id SERIAL PRIMARY KEY, title VARCHAR(255) NOT NULL, content TEXT NOT NULL, author VARCHAR(255) NOT NULL, date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP );
Start the server ( Here nodemon is required ) :
nodemon server.jsThe API will be available at http://localhost:3000.
| Method | Endpoint | Description |
|---|---|---|
| GET | /posts | Retrieve all posts |
| GET | /posts/:id | Retrieve a specific post |
| POST | /posts | Create a new post |
| PATCH | /posts/:id | Update a specific post |
| DELETE | /posts/:id | Delete a specific post |
Here are some example requests you can run in Postman:
- Method: POST
- URL:
http://localhost:3000/posts - Headers:
Content-Type: application/json - Body:
{ "title": "My First Post", "content": "This is the content of my first post", "author": "John Doe" }
- Method: GET
- URL:
http://localhost:3000/posts
- Method: GET
- URL:
http://localhost:3000/posts/1
- Method: PATCH
- URL:
http://localhost:3000/posts/1 - Headers:
Content-Type: application/json - Body:
{ "title": "Updated Title" }
- Method: DELETE
- URL:
http://localhost:3000/posts/1
The API uses conventional HTTP response codes to indicate the success or failure of an API request:
- 200: Success
- 201: Created (For POST requests)
- 204: No Content (For successful DELETE requests)
- 404: Not Found
- 500: Server Error
If an error occurs, the API will return a JSON object with a message field describing the error.
This project is open source and available under the MIT License.