Introduction to the MERN Stack with Example Project




Introduction to the MERN Stack with Example Project

The MERN Stack is a powerful combination of technologies used to develop full-stack web applications. It consists of:

  • MongoDB – NoSQL database
  • Express.js – Backend framework
  • React.js – Frontend library
  • Node.js – JavaScript runtime environment

Together, these technologies provide a seamless development experience, allowing developers to build fast and scalable applications using JavaScript for both frontend and backend.


Why Choose the MERN Stack?

  1. Full JavaScript Development – Use a single programming language for frontend and backend.
  2. Scalability – MongoDB allows flexible and scalable database management.
  3. Component-Based UI – React simplifies UI development with reusable components.
  4. Fast Performance – Node.js handles asynchronous operations efficiently.
  5. Large Community Support – Extensive resources and support from developers worldwide.

Example MERN Stack Project: A Simple To-Do App

Let's build a basic To-Do List application using the MERN stack.

1. Setting Up the Backend (Node.js + Express.js + MongoDB)

Step 1: Initialize a new project and install dependencies

mkdir mern-todo-app && cd mern-todo-app
npm init -y
npm install express mongoose cors dotenv

Step 2: Create a simple Express server (server.js)

const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");
const app = express();
app.use(cors());
app.use(express.json());

mongoose.connect("mongodb://localhost:27017/todo", {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

const todoSchema = new mongoose.Schema({ task: String });
const Todo = mongoose.model("Todo", todoSchema);

app.get("/todos", async (req, res) => {
  const todos = await Todo.find();
  res.json(todos);
});

app.post("/todos", async (req, res) => {
  const newTodo = new Todo({ task: req.body.task });
  await newTodo.save();
  res.json(newTodo);
});

app.listen(5000, () => console.log("Server running on port 5000"));

2. Setting Up the Frontend (React.js)

Step 1: Create a React app

npx create-react-app client
cd client
npm install axios

Step 2: Fetch and display data in App.js

import React, { useEffect, useState } from "react";
import axios from "axios";

function App() {
  const [todos, setTodos] = useState([]);
  const [task, setTask] = useState("");

  useEffect(() => {
    axios.get("http://localhost:5000/todos").then((response) => {
      setTodos(response.data);
    });
  }, []);

  const addTodo = () => {
    axios.post("http://localhost:5000/todos", { task }).then((response) => {
      setTodos([...todos, response.data]);
      setTask("");
    });
  };

  return (
    <div>
      <h1>To-Do List</h1>
      <input value={task} onChange={(e) => setTask(e.target.value)} />
      <button onClick={addTodo}>Add</button>
      <ul>
        {todos.map((todo, index) => (
          <li key={index}>{todo.task}</li>
        ))}
      </ul>
    </div>
  );
}

export default App;

Conclusion

The MERN stack is an excellent choice for full-stack development. With just JavaScript, you can build a dynamic and responsive web application. This To-Do List app is a basic example, but you can extend it with authentication, real-time updates, and more advanced features.

Start experimenting with the MERN stack and build your own amazing projects!


Do you have any questions or want more tutorials? Let me know in the comments!

 

Comments

Popular posts from this blog

The Future of Technology

Understanding Data Structures: The Backbone of Efficient Programming

The Power of Coding