A secure blogging platform built with Node.js, Express, MongoDB, and EJS, featuring user authentication, blog management, and a simple frontend interface.
- JWT Authentication (signup/login)
- Full Blog CRUD Operations
- Reading time calculation (auto-calculated based on content length)
- Draft/Published state control
- Read count tracking (increases on each view)
- Owner-only edit/delete access
- EJS-powered frontend views with CSS styling
- Comprehensive testing setup for core functionality
git clone https://github.com/your-username/blogging-api.git
cd blogging-api
npm install
Create a .env file in the root directory:
PORT=5000
MONGODB_URI=mongodb://localhost:27017/blogging-api
JWT_SECRET=your-super-secret-jwt-key-here-make-it-long-and-random
JWT_EXPIRES_IN=1hSecurity Tip: Generate a strong secret key using:
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
Ensure MongoDB is running locally:
mongod
Use cloud-hosted MongoDB:
MONGODB_URI=mongodb+srv://<username>:<password>@cluster0.abcd.mongodb.net/blogging-api?retryWrites=true&w=majoritynpm run dev
npm start
The server will run at http://localhost:${PORT}
| Method | Route | Description | Access |
|---|---|---|---|
| GET | / |
Get all published blogs | Public |
| POST | /auth/signup |
Register a new user | Public |
| POST | /auth/login |
Login and get JWT token | Public |
| GET | /dashboard |
User dashboard (EJS) | Authenticated Users |
| POST | /blogs/create |
Create a new blog | Authenticated Users |
| PUT | /blogs/:id/edit |
Update an existing blog | Blog owner Only |
| DELETE | /blogs/:id |
Delete a blog | Blog owner Only |
| GET | /blogs/:id |
View a specific blog | Public |
Signup
POST http://localhost:5000/auth/signup
Content-Type: application/json
{
"first_name": "John",
"last_name": "Doe",
"email": "john@example.com",
"password": "password123"
}
Login
POST http://localhost:5000/auth/login
Content-Type: application/json
{
"email": "john@example.com",
"password": "password123"
}
Create a Blog
POST http://localhost:5000/blogs/create
Authorization: Bearer YOUR_JWT_TOKEN
Content-Type: application/json
{
"title": "My First Blog",
"content": "This is the content of my first blog post."
}
Get All Published Blogs
GET http://localhost:5000/
View Specific Blog
GET http://localhost:5000/blogs/64a7b9e1f8d8e42c12345678
View the web interface at:
http://localhost:5000/auth/signup– Registration pagehttp://localhost:5000/auth/login– Login pagehttp://localhost:5000/dashboard– User dashboardhttp://localhost:5000/blogs/create– Create blog form
After login, tokens are automatically stored in browser localStorage.
Personally deployed to Pipeops with MongoDB Atlas, but you can use other PAAS and MongoDB cloud providers and it (probably) won't break lol
Set these variables in your PaaS dashboard:
MONGODB_URI– Your MongoDB connection stringJWT_SECRET– Secure JWT signing keyPORT– Port number (optional as it defaults to 5000)