A full-stack blogging platform built with Node.js, Express, and MongoDB. Users can sign up, log in, create blog posts with cover images, and comment on posts. The app uses server-rendered EJS views on the front end and a cookie/JWT-based authentication system on the back end.
- 🔐 User Authentication — Signup and login with hashed passwords (bcrypt) and JWT-based session tokens stored in HTTP cookies
- 📰 Blog Management — Create blog posts with a title, body, and cover image upload
- 💬 Comments — Authenticated users can comment on blog posts
- 🖼️ Image Uploads — Cover images handled via Multer, stored on disk and served as static assets
- 🛡️ Middleware-based Auth Guard — Requests are checked for a valid auth cookie and the user is attached to
req.user - 🗂️ MVC-style Architecture — Clean separation between routes, controllers, models, and services
| Layer | Technology |
|---|---|
| Runtime | Node.js |
| Framework | Express.js |
| Database | MongoDB with Mongoose |
| Templating | EJS |
| Authentication | JSON Web Tokens (JWT) + Cookies |
| File Uploads | Multer |
| Password Hashing | bcrypt |
| Env Config | dotenv |
nodejs-blog-rest-api/
├── controller/ # Route handler logic
│ ├── blog.js # Create blog, view blog, add comment
│ └── user.js # Signup, login, logout
├── middleware/
│ ├── blogmulter.js # Multer config for cover image uploads
│ └── checkauth.js # Verifies JWT cookie and attaches req.user
├── model/ # Mongoose schemas
│ ├── blog.js
│ ├── comment.js
│ └── user.js
├── router/ # Express route definitions
│ ├── blog.js
│ ├── static.js
│ └── user.js
├── service/
│ └── authentication.js # JWT sign/verify helpers
├── views/ # EJS templates
│ ├── home.ejs
│ ├── blog.ejs
│ ├── login.ejs
│ ├── signup.ejs
│ ├── addblog.ejs
│ └── partials/
├── public/ # Static assets & uploaded images
├── index.js # App entry point
└── package.json
-
Clone the repository
git clone https://github.com/Aman-20/nodejs-blog-rest-api.git cd nodejs-blog-rest-api -
Install dependencies
npm install
-
Configure environment variables
Create a
.envfile in the project root:PORT=3000 MONGO_URL=your_mongodb_connection_string
-
Run the app
npm start
-
Open your browser at
http://localhost:3000
| Method | Endpoint | Description |
|---|---|---|
| POST | /user/signup |
Register a new user |
| POST | /user/login |
Log in and receive an auth cookie |
| GET | /user/logout |
Clear the auth cookie and log out |
| Method | Endpoint | Description |
|---|---|---|
| GET | /blog/addblog |
Render the "create new blog" form |
| POST | /blog |
Create a new blog post (with cover image) |
| GET | /blog/:id |
View a single blog post and its comments |
| POST | /blog/comment/:blogId |
Add a comment to a blog post |
| Method | Endpoint | Description |
|---|---|---|
| GET | / |
Home page — lists all blog posts |
| GET | /login |
Render the login page |
| GET | /signup |
Render the signup page |
- On signup, the user's password is hashed with bcrypt before being saved to MongoDB.
- On login, the submitted password is compared against the stored hash.
- On successful login, a JWT is generated and stored in an HTTP cookie.
- A custom
checkForAuthenticationcookiesmiddleware runs on every request, verifies the token, and attaches the decoded user toreq.userso it's available in controllers and views.
- Add input validation (e.g. with
express-validatororzod) - Add edit/delete functionality for blog posts and comments
- Add pagination for the blog listing page
- Write automated tests (unit + integration)
- Convert to a pure JSON REST API with a separate front-end (e.g. React) if a fully decoupled architecture is desired
Aman GitHub: @Aman-20
This project is licensed under the ISC License.