In short
This is where everything in Phases 3 to 7 comes together. You rebuild your blog API on MongoDB with real models, validation, populate, hooks and pagination, and end up with a backend worth showing in interviews.
Rebuild your Phase 4 blog API on MongoDB with Mongoose models for users, posts and comments, using validation, populate, hooks and pagination.

Think of it like this
Running a college magazine
Writers submit articles (posts), editors check them against the style guide (validation), readers leave letters (comments), every article credits its writer (populate) and the index shows ten articles per page (pagination). You are building the office that runs it all.
Watch it happen
Project structure
1/4Three models with validation, timestamps and references between them.
The big ideas
What you need to know
Three related models
Post references User as author. Comment references Post and User. Index post and author fields.
Validate everything
Title length, required body, slug format and allowed status values like draft or published.
Populate for responses
Lists show the author's name. A single post shows comments with their authors.
Paginate and filter
GET /posts supports page, limit, tag and search, using your helpers from earlier lessons.
How it works
Build order
- 1
Connect and structure
Set up app.js, server.js, Mongoose connection and folders.
- 2
Models
User with hashed password, Post with slug and status, Comment with references.
- 3
Routes and controllers
Posts CRUD, comments under /posts/:id/comments, users register.
- 4
Errors and validation
AppError, error middleware handling ValidationError, CastError and 11000.
- 5
Polish
Pagination, filters, seed script, README and Postman collection.
See it in code
Walk through the code
1// src/models/post.model.js2const postSchema = new Schema(3 {4 title: { type: String, required: true, trim: true, minLength: 5, maxLength: 120 },5 slug: { type: String, unique: true, lowercase: true },6 body: { type: String, required: true },7 author: { type: Schema.Types.ObjectId, ref: "User", required: true, index: true },8 tags: { type: [String], index: true },9 status: { type: String, enum: ["draft", "published"], default: "draft" },10 },11 { timestamps: true }12);13 14postSchema.pre("validate", function () {15 if (this.isModified("title")) {16 this.slug = this.title.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/(^-|-$)/g, "");17 }18});19 20// src/controllers/post.controller.js21export async function getPost(req, res) {22 const post = await Post.findOne({ slug: req.params.slug, status: "published" })23 .populate("author", "firstName lastName");24 if (!post) throw new AppError(404, "NOT_FOUND", "Post not found");25 const comments = await Comment.find({ post: post._id }).populate("author", "firstName").sort({ createdAt: 1 }).lean();26 res.json({ post, comments });27}1/6Title rules, a unique slug for clean URLs and a required body.
The heart of the project: a validated Post model and a controller that returns a post with its comments.
Save yourself the pain
Common mistakes
Oops
Letting any logged-out request create or delete posts.
Do this instead
Even before you add login in a later phase, plan which routes will need auth and mark them in the README.
Oops
Returning Mongoose CastError messages for bad ids like /posts/abc.
Do this instead
Handle CastError in error middleware and reply 400 with "Invalid id".
Oops
Pushing without a seed script, so reviewers see an empty API.
Do this instead
Add npm run seed that creates sample users, posts and comments.
Your mission
Ship the blog API
Quick check
A post's slug is built in a pre('validate') hook rather than pre('save'). Why?
Go deeper
- 1Reference the author in each post and populate it in list and detail routes.
- 2Add a slug generated in a pre('save') hook.
- 3Seed the database with sample posts using a script.
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.