⬅ Previous Topic
Project 1: Task Tracker with Node.js + MongoDBNext Topic ⮕
Project 3: Inventory System (Console-based App)⬅ Previous Topic
Project 1: Task Tracker with Node.js + MongoDBNext Topic ⮕
Project 3: Inventory System (Console-based App)In this project, you'll build a basic Blog Application using Node.js, Express, and MongoDB via the Mongoose ODM. You'll be able to create, read, and display blog posts dynamically.
The app will allow users to:
blog-app/
├── models/
│ └── Post.js
├── views/
│ ├── index.ejs
│ ├── new.ejs
│ └── show.ejs
├── public/
├── app.js
├── package.json
mkdir blog-app
cd blog-app
npm init -y
npm install express mongoose ejs body-parser
Create app.js
with the following code:
// app.js
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const Post = require("./models/Post");
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.set("view engine", "ejs");
mongoose.connect("mongodb://localhost:27017/blogApp", {
useNewUrlParser: true,
useUnifiedTopology: true,
});
app.listen(3000, () => {
console.log("Server started on http://localhost:3000");
});
Q: Why use Mongoose instead of MongoDB native driver?
A: Mongoose provides schema enforcement, easy model creation, and helpful methods that simplify working with MongoDB from Node.js.
Create models/Post.js
:
// models/Post.js
const mongoose = require("mongoose");
const postSchema = new mongoose.Schema({
title: String,
body: String,
createdAt: { type: Date, default: Date.now },
});
module.exports = mongoose.model("Post", postSchema);
Output:
Post model created with schema: title, body, createdAt
Add the following routes to app.js
to list, create, and show posts:
// app.js (continued)
// Home - list all posts
app.get("/", async (req, res) => {
const posts = await Post.find().sort({ createdAt: -1 });
res.render("index", { posts });
});
// Show form to create a post
app.get("/new", (req, res) => {
res.render("new");
});
// Save post to database
app.post("/posts", async (req, res) => {
const { title, body } = req.body;
await Post.create({ title, body });
res.redirect("/");
});
// Show individual post
app.get("/posts/:id", async (req, res) => {
const post = await Post.findById(req.params.id);
res.render("show", { post });
});
views/index.ejs
<h1>All Blog Posts</h1>
<a href="/new">Create New Post</a>
<ul>
<% posts.forEach(post => { %>
<li>
<a href="/posts/<%= post._id %>"><%= post.title %></a>
</li>
<% }) %>
</ul>
views/new.ejs
<h1>New Blog Post</h1>
<form method="POST" action="/posts">
<input name="title" placeholder="Post Title"/><br/>
<textarea name="body" placeholder="Post Body"></textarea><br/>
<button type="submit">Create Post</button>
</form>
views/show.ejs
<h1><%= post.title %></h1>
<p><%= post.body %></p>
<a href="/">Back to Home</a>
node app.js
Output:
Server started on http://localhost:3000
http://localhost:3000
to see all postsQ: Why use EJS instead of plain HTML?
A: EJS lets you dynamically inject data from your MongoDB database into your HTML templates. It's like templating HTML with logic!
This project taught you how to build a basic blog app using Express, Mongoose, and MongoDB. You learned how to model data, create RESTful routes, render views using EJS, and interact with MongoDB using Mongoose in a beginner-friendly way.
Try adding more features like post editing, deletion, or even user authentication using Passport.js!
⬅ Previous Topic
Project 1: Task Tracker with Node.js + MongoDBNext Topic ⮕
Project 3: Inventory System (Console-based App)You can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.