Backend

Blog App in Express JS | Project Setup and Models Creation

September 11, 2024

Building a Simple Node.js Application with Express and Mongoose

In this blog, we'll explore how to build a simple Node.js application using Express for handling HTTP requests and Mongoose for interacting with a MongoDB database. This project includes user authentication and blog post functionality.

Tools & Libraries

  • Express: A lightweight framework for creating web servers and APIs in Node.js.
  • Mongoose: A library that helps in managing MongoDB by providing a schema-based solution.
  • Nodemon: A tool that automatically restarts the server when changes are made to the code.

Setting Up the Project

We start by installing the necessary packages using npm:

npm install express mongoose nodemon

Then, we add "type": "module" in package.json to allow the use of ES6 imports in our Node.js project.

The Structure of the Application

We will have the following file structure:

  • src/index.js (Entry point of the application)
  • src/model/UserModel.js (User schema for MongoDB)
  • src/model/BlogModel.js (Blog schema for MongoDB)

1. Setting up the Express Server (src/index.js)

In the main file (index.js), we import Express and body-parser to handle HTTP requests.

import express from 'express';
import bodyParser from 'body-parser';

const app = express();
app.use(bodyParser.json());
app.get('/', (req, res) => {
    res.send('Hello World');
});
app.listen(3000, () => {
    console.log('Server is running on http://localhost:3000');
});

Next, we define a simple route: Finally, the server listens on port 3000:

2. Creating the User Model (src/model/UserModel.js)

The UserModel is used to handle user data, including fields such as name, email, password, and role. Additionally, we store information for password recovery.

import mongoose, { Schema } from "mongoose";

const UserSchema = new Schema({
    name: {
        type: String,
        required: true,
    },
    email: {
        type: String,
        required: true,
    },
    password: {
        type: String,
        required: true,
    },
    role: {
        type: String,
        required: true,
        default: "user",
    },
    forgotPassword: {
        type: String,
    },
    forgotPasswordExpires: {
        type: Date,
    },
}, { timestamps: true });

export const UserModel = mongoose.model("User", UserSchema);
  • name, email, and password are required fields.
  • role has a default value of "user".
  • forgotPassword and forgotPasswordExpires are used for password recovery.

3. Creating the Blog Model (src/model/BlogModel.js)

The BlogModel stores blog post data such as title, content, and tags. Here's the schema:

import mongoose, { Schema } from "mongoose";

const BlogSchema = new Schema({
    title: {
        type: String,
        required: true,
    },
    content: {
        type: String,
        required: true,
    },
    tags: {
        type: [String],
    },
}, { timestamps: true });

export const BlogModel = mongoose.model("Blog", BlogSchema);
  • title and content are required fields.
  • tags is an optional array to store blog tags.

Running the Application

To run this app, you can use nodemon, which automatically restarts the server when changes are made. You can start the server with:

npx nodemon src/index.js //or
npm run dev

Visit http://localhost:3000 in your browser, and you should see the message "Hello World".

Conclusion

This is a basic setup for a Node.js application using Express and Mongoose. The project includes a user model and a blog post model, which can be expanded further to build a more robust system. Follow for more features such as user authentication, routes for creating blogs, and more advanced functionality to build a complete application!

Thank you for reading 😁