Pagination and Search
Goal
This snapshot advances Copperline Journal by teaching you to paginate the feed and search indexed post text.
Every numbered folder is a complete app. Run this stage independently, then compare it with the previous folder to see the new responsibility clearly.
Prerequisites
Use Node.js 20 or newer, npm, and a terminal. You should recognize basic JavaScript functions, objects, and HTTP requests.
Port 3000 must be available. MongoDB snapshots use Docker locally; copy the example environment file before starting them.
- Node.js and npm installed
- A code editor and terminal
- Docker from part 4 onward
Concepts
Pagination bounds database work with skip and limit. A MongoDB text index supports search across chosen public fields.
Keep responsibilities visible: middleware prepares requests, routes choose handlers, models protect data rules, and EJS views present escaped values.
Walkthrough
Clamp the page number, preserve the search query in pager links, count the filtered result set, and return five posts per page.
Read the example from input to output, then inspect the matching snapshot. The repository includes the surrounding setup and error handling.
const filter = q ? { $text: { $search: q } } : {};
const posts = await Post.find(filter).skip((page - 1) * 5).limit(5);
Run and verify
Enter 10-Pagination-And-Search, install its dependencies, copy .env.example when present, and start the server.
Open http://localhost:3000. Keep the terminal visible so route, validation, and database errors can be connected to the browser action that caused them.
docker compose up -d
git clone https://github.com/michaeldunga1/fcc-express-blog.git
cd fcc-express-blog/10-Pagination-And-Search
npm install
cp .env.example .env
npm start
# optional: npm run seed
Troubleshooting
Use the same filter for count and find or page totals become wrong. Bound query length and reject nonsensical page values.
A missing-module error usually means npm install ran in another snapshot. For database failures, check the container and that this folder uses its own Copperline database name.
- Read the first error first
- Restart after environment changes
- Never commit .env or node_modules
Try this
Seed enough stories for three pages and search for a term owned by only one author.
Test a happy path and one invalid or unauthorized request. Useful applications return clear feedback without exposing secrets or stack traces.
- Make one small change
- Test it in the browser
- Compare with the next snapshot only after it works
Next: Password Reset
Comments
One comment per signed-in account. Comments are saved with this page’s URL.