Building a Full-Stack Blogging Platform with Node.js, Express.js, and MongoDB

Building a Full-Stack Blogging Platform with Node.js, Express.js, and MongoDB

Introduction

Express.js is a fast, unopinionated, minimalist web framework for Node.js used for building web applications and APIs. This tutorial will guide you in creating a full-featured blogging platform using Express.js, Node.js, and MongoDB.

Prerequisites

  • Node.js and NPM installed on your machine.
  • MongoDB installed or MongoDB Atlas account.
  • Basic understanding of JavaScript and MongoDB.
  • An IDE or a code editor like Visual Studio Code.

Part 1: Setting Up the Project

Create a new directory for your project, initialize a new Node.js project, and install the required dependencies.

  1. mkdir blog-platform
  2. cd blog-platform
  3. npm init -y
  4. npm install express mongoose ejs body-parser

Part 2: Setting Up Express.js

Create an `app.js` file and set up a basic Express.js server.

  1. const express = require('express');
  2. const app = express();
  3.  
  4. app.listen(3000, () => {
  5. console.log('Server listening on port 3000');
  6. });

Part 3: Setting Up MongoDB with Mongoose

Connect to MongoDB using Mongoose.

  1. const mongoose = require('mongoose');
  2. mongoose.connect('mongodb://localhost/blog-platform', { useNewUrlParser: true, useUnifiedTopology: true });

Part 4: Setting Up Routes

Set up basic routes for viewing and creating blog posts.

  1. app.get('/', (req, res) => {
  2. // code to retrieve and display posts
  3. });
  4.  
  5. app.get('/new', (req, res) => {
  6. // code to display form to create new post
  7. });
  8.  
  9. app.post('/new', (req, res) => {
  10. // code to create new post
  11. });

Part 5: Creating and Displaying Blog Posts

Create a Mongoose schema for blog posts and use it to retrieve and display posts.

  1. const blogSchema = new mongoose.Schema({
  2. title: String,
  3. content: String,
  4. });
  5.  
  6. const Blog = mongoose.model('Blog', blogSchema);
  7.  
  8. app.get('/', (req, res) => {
  9. Blog.find({}, (err, posts) => {
  10. if (err) {
  11. console.error(err);
  12. } else {
  13. // Render the posts
  14. }
  15. });
  16. });
  17.  
  18. app.get('/new', (req, res) => {
  19. // render the form to create a new post
  20. });
  21.  
  22. app.post('/new', (req, res) => {
  23. const blog = new Blog({
  24. title: req.body.title,
  25. content: req.body.content
  26. });
  27.  
  28. blog.save(err => {
  29. if (err) {
  30. console.error(err);
  31. } else {
  32. res.redirect('/');
  33. }
  34. });
  35. });

Part 6: Adding a View Engine

Use EJS as a templating engine for creating views.

  1. app.set('view engine', 'ejs');

Part 7: Serving Static Files

Express.js can serve static files such as images, CSS files, and JavaScript files in the public directory.

  1. app.use(express.static('public'));

Conclusion

In this tutorial, we've walked through creating a simple full-stack blogging platform with Node.js, Express.js, and MongoDB. We covered the basics of setting up an Express.js server, connecting to a MongoDB database using Mongoose, setting up basic routing for our application, and implementing a simple blog post model.

This tutorial is a starting point, and there's plenty more to explore. You could add user authentication, comments, likes, or any other features that you'd like. Happy coding!

We use cookies to improve your browsing experience. By continuing to use this website, you consent to our use of cookies. Learn More