build a blog web app
- User
build a blog web app
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
build a blog web app
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Building a blog web app involves several steps, including setting up the backend, frontend, and database. Below is a high-level guide to help you build a basic blog web application using common modern technologies:
mkdir blog-app
cd blog-app
npm init -y
npm install express mongoose ejs body-parser dotenv
npm install --save-dev nodemon
Add script to package.json:
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
}
/blog-app
│
├── models/
│ └── Post.js
├── views/
│ ├── index.ejs
│ ├── edit.ejs
│ ├── show.ejs
│ └── new.ejs
├── public/
│ └── style.css
├── .env
├── index.js
└── package.json
Use MongoDB Atlas or local MongoDB.
Create .env file:
PORT=3000
MONGO_URI=your_mongodb_connection_string
index.js)const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const Post = require('./models/Post');
require('dotenv').config();
const app = express();
// Middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
app.set('view engine', 'ejs');
// Connect to DB
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });
// Routes
// Home
app.get('/', async (req, res) => {
const posts = await Post.find().sort({ createdAt: 'desc' });
res.render('index', { posts });
});
// New Post Form
app.get('/posts/new', (req, res) => {
res.render('new');
});
// Create Post
app.post('/posts', async (req, res) => {
const { title, content } = req.body;
await Post.create({ title, content });
res.redirect('/');
});
// Show Single Post
app.get('/posts/:id', async (req, res) => {
const post = await Post.findById(req.params.id);
res.render('show', { post });
});
// Edit Post Form
app.get('/posts/:id/edit', async (req, res) => {
const post = await Post.findById(req.params.id);
res.render('edit', { post });
});
// Update Post
app.post('/posts/:id', async (req, res) => {
const { title, content } = req.body;
await Post.findByIdAndUpdate(req.params.id, { title, content });
res.redirect('/');
});
// Delete Post
app.post('/posts/:id/delete', async (req, res) => {
await Post.findByIdAndDelete(req.params.id);
res.redirect('/');
});
// Start server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server running on http://localhost:${PORT}`));
models/Post.js)const mongoose = require('mongoose');
const PostSchema = new mongoose.Schema({
title: String,
content: String,
createdAt: {
type: Date,
default: Date.now
}
});
module.exports = mongoose.model('Post', PostSchema);
views/index.ejs<!DOCTYPE html>
<html>
<head><title>My Blog</title></head>
<body>
<h1>Blog Posts</h1>
<a href="/posts/new">New Post</a>
<ul>
<% posts.forEach(post => { %>
<li>
<a href="/posts/<%= post._id %>"><%= post.title %></a>
<a href="/posts/<%= post._id %>/edit">Edit</a>
<form action="/posts/<%= post._id %>/delete" method="POST">
<button type="submit">Delete</button>
</form>
</li>
<% }) %>
</ul>
</body>
</html>
views/new.ejs<h1>New Post</h1>
<form action="/posts" method="POST">
<input name="title" placeholder="Title" required />
<br/>
<textarea name="content" placeholder="Content" required></textarea>
<br/>
<button type="submit">Save</button>
</form>
views/edit.ejs<h1>Edit Post</h1>
<form action="/posts/<%= post._id %>" method="POST">
<input name="title" value="<%= post.title %>" required />
<br/>
<textarea name="content" required><%= post.content %></textarea>
<br/>
<button type="submit">Update</button>
</form>
views/show.ejs<h1><%= post.title %></h1>
<p><%= post.content %></p>
<a href="/">Back</a>
Put your CSS in /public/style.css and link it in EJS templates:
<link rel="stylesheet" href="/style.css" />
Start dev server:
npm run dev
Open browser at:
http://localhost:3000
To allow only logged-in users to create/edit/delete posts, integrate:
You can deploy this app using:
If you want to containerize your blog app, I can provide a Dockerfile and docker-compose.yml.
Would you like me to generate a complete ZIP of this project or help you convert this into a React-based frontend or Next.js app?