Skip to content

About

RESTful blog backend build with Node.js & Express - JWT auth, CRUD post, comments and image uploads via Multer/Cloudinary.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

📝 Node.js Blog REST API

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.

✨ Features

  • 🔐 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

🛠️ Tech Stack

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

📁 Project Structure

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

🚀 Getting Started

Prerequisites

  • Node.js (v16 or higher recommended)
  • A running MongoDB instance (local or Atlas)

Installation

  1. Clone the repository

    git clone https://github.com/Aman-20/nodejs-blog-rest-api.git
    cd nodejs-blog-rest-api
  2. Install dependencies

    npm install
  3. Configure environment variables

    Create a .env file in the project root:

    PORT=3000
    MONGO_URL=your_mongodb_connection_string
  4. Run the app

    npm start
  5. Open your browser at http://localhost:3000

🔌 API Routes

User Routes (/user)

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

Blog Routes (/blog)

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

Static Routes (/)

Method Endpoint Description
GET / Home page — lists all blog posts
GET /login Render the login page
GET /signup Render the signup page

🔒 Authentication Flow

  1. On signup, the user's password is hashed with bcrypt before being saved to MongoDB.
  2. On login, the submitted password is compared against the stored hash.
  3. On successful login, a JWT is generated and stored in an HTTP cookie.
  4. A custom checkForAuthenticationcookies middleware runs on every request, verifies the token, and attaches the decoded user to req.user so it's available in controllers and views.

🧭 Improvements Roadmap

  • Add input validation (e.g. with express-validator or zod)
  • 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

👤 Author

Aman GitHub: @Aman-20

📄 License

This project is licensed under the ISC License.

About

RESTful blog backend build with Node.js & Express - JWT auth, CRUD post, comments and image uploads via Multer/Cloudinary.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Contributors

Languages