A RESTful Blog API built with Node.js, Express.js, and PostgreSQL. It provides full CRUD functionality for blog posts along with keyword search, pagination, input validation, centralized error handling, and SQL injection prevention using parameterized queries.
Base URL
https://blog-api-56im.onrender.com
- Runtime: Node.js
- Framework: Express.js
- Database: PostgreSQL
- Driver: pg
- Testing: Postman
- Deployment: Render
- Version Control: Git & GitHub
# π Project Structure
```text
blog-api/
β
βββ src/
β βββ config/
β β βββ db.js
β βββ middleware/
β β βββ validatePost.js
β β βββ errorHandler.js
β βββ routes/
β β βββ posts.js
β βββ utils/
β β βββ response.js
β βββ app.js
β
βββ screenshots/
β βββ health-check.png
β βββ create-post.png
β βββ get-all-posts.png
β βββ search-posts.png
β βββ validation-error.png
β βββ delete-post.png
β
βββ interview-prep/
β βββ project2-interview-questions.md
β
βββ schema.sql
βββ server.js
βββ .env
βββ .gitignore
βββ package.json
βββ README.md
---
# β¨ Features
* Full CRUD operations for blog posts
* Keyword search across title and content
* Pagination with metadata
* Manual input validation
* Parameterized SQL queries
* SQL injection prevention
* Centralized error handling
* Standardized JSON response format
* Production deployment on Render
---
# π» Local Setup
## Prerequisites
* Node.js v18+
* PostgreSQL installed locally
## Steps
### 1. Clone the repository
```bash
git clone https://github.com/Harshavardhan3535/blog-api.git
cd blog-api
npm installPORT=5000
DATABASE_URL=postgresql://postgres:yourpassword@localhost:5432/blog_api
NODE_ENV=developmentCreate a database named:
blog_api
Run the provided:
schema.sql
using pgAdmin Query Tool or psql.
npm run dev| Method | Endpoint | Description |
|---|---|---|
| GET | /health |
Check API status |
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/posts |
Create a new post |
| GET | /api/posts |
Get all posts |
| GET | /api/posts?page=1&limit=10 |
Pagination |
| GET | /api/posts?search=keyword |
Keyword search |
| GET | /api/posts?search=keyword&page=1&limit=5 |
Search + Pagination |
| GET | /api/posts/:id |
Get post by ID |
| PUT | /api/posts/:id |
Update post |
| DELETE | /api/posts/:id |
Delete post |
The API was tested using Postman.
The following functionality has been verified:
- Create Post
- Get All Posts
- Get Single Post
- Update Post
- Delete Post
- Keyword Search
- Pagination
- Input Validation
- Error Responses
POST /api/posts
{
"title": "My First Post",
"content": "This is the content of my first blog post.",
"author": "Harsha Vardhan"
}{
"success": true,
"message": "Post created successfully",
"data": {
"id": 1,
"title": "My First Post",
"content": "This is the content of my first blog post.",
"author": "Harsha Vardhan",
"created_at": "2024-01-01T10:00:00.000Z",
"updated_at": "2024-01-01T10:00:00.000Z"
}
}{
"success": true,
"message": "Posts fetched successfully",
"pagination": {
"currentPage": 1,
"totalPages": 3,
"totalPosts": 25,
"limit": 10
},
"data": []
}{
"success": false,
"errors": [
"Title is required",
"Content must be at least 10 characters"
]
}| Variable | Description |
|---|---|
| PORT | Server Port |
| DATABASE_URL | PostgreSQL Connection String |
| NODE_ENV | development / production |
- Initialized Node.js project
- Configured Express server
- Connected PostgreSQL database
- Created folder structure
- Installed dependencies
- Express project structure
- PostgreSQL connection pooling
- Environment variables
- Database configuration
- Full CRUD operations
- Parameterized SQL queries
- Input validation middleware
- Pagination using LIMIT and OFFSET
Used PostgreSQL parameterized queries ($1, $2) instead of string concatenation to safely execute SQL statements.
Created middleware to validate request payloads before performing database operations.
Implemented LIMIT and OFFSET to efficiently return paginated results.
- CRUD API design
- RESTful routing
- Parameterized queries
- SQL Injection Prevention
- Express middleware
- Pagination
- DELETE endpoint
- Keyword search using PostgreSQL ILIKE
- Search with pagination
- Response helper utilities
- Centralized error middleware
- Express
next(error)implementation
Created reusable response helper functions to eliminate duplicated JSON response code.
Implemented centralized Express error middleware instead of handling errors inside every route.
Combined ILIKE search with LIMIT/OFFSET and COUNT queries to support searchable paginated results.
- Express Error Middleware
- Centralized Error Handling
- Response Helpers
- PostgreSQL ILIKE
- Search with Pagination
- Separation of Concerns
- Created schema.sql
- Configured SSL support
- Connected PostgreSQL on Render
- Deployed API to Render
- Configured environment variables
- Production deployment requires proper folder structure.
- Environment variables are critical.
- Cloud database credentials must be configured correctly.
- Local development success does not always guarantee successful production deployment.
- REST API Design
- CRUD Operations
- Express.js
- PostgreSQL
- SQL Queries
- Parameterized Queries
- SQL Injection Prevention
- Pagination
- Keyword Search
- Input Validation
- Express Middleware
- Centralized Error Handling
- Response Helpers
- Environment Variables
- Production Deployment
- Render
- Postman API Testing
- JWT Authentication
- User Registration & Login
- Role-Based Access Control (RBAC)
- Image Upload Support
- Comments API
- Swagger / OpenAPI Documentation
- Docker Support
- Unit & Integration Testing
Harsha Vardhan
GitHub: https://github.com/Harshavardhan3535
This project is licensed under the MIT License.






