DEV Community

Cover image for How to Build Your First Full-Stack Application with MERN in 7 Days
Raji moshood
Raji moshood

Posted on

How to Build Your First Full-Stack Application with MERN in 7 Days

Have you ever dreamed of creating a complete web application from scratch but didn’t know where to start? With the MERN stack (MongoDB, Express.js, React, Node.js), you can build a full-stack app in just 7 days—even if you’re new to web development! Let’s break it down, step by step.

What is the MERN Stack?

The MERN stack is a JavaScript-based framework for building full-stack web applications. Here’s a quick overview:

MongoDB: A NoSQL database for storing data in JSON-like documents.

Express.js: A lightweight framework for building back-end APIs.

React: A front-end library for building interactive user interfaces.

Node.js: A JavaScript runtime for running server-side code.

By using JavaScript end-to-end, the MERN stack allows seamless communication between the front-end and back-end.

Day-by-Day Plan to Build Your First MERN App

Day 1: Project Setup and Environment Configuration

Goals:

Install the required tools and create the project structure.

Steps:

  1. Install Node.js: Download and install Node.js from the official website.

  2. Set up your IDE: Use VS Code for development.

  3. Initialize the project:

mkdir mern-todo-app
cd mern-todo-app
npm init -y

Enter fullscreen mode Exit fullscreen mode
  1. Install Dependencies:

Backend: express, mongoose, dotenv.

Frontend: react, react-dom, axios.

  1. Folder Structure:
mern-todo-app/
├── backend/
├── frontend/
├── .env
├── package.json
├── README.md


Enter fullscreen mode Exit fullscreen mode

Day 2: Build the Back-End with Node.js and Express.js

Goals:

Set up a RESTful API for handling tasks.

Steps:

  1. Create a server.js file:
const express = require("express");
const mongoose = require("mongoose");
const dotenv = require("dotenv");

dotenv.config();
const app = express();

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

Enter fullscreen mode Exit fullscreen mode
  1. Connect to MongoDB: Use the MongoDB Atlas free tier or run a local instance.
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("MongoDB connected"))
.catch(err => console.error(err));


Enter fullscreen mode Exit fullscreen mode
  1. Create a Task Model:
const mongoose = require("mongoose");

const taskSchema = new mongoose.Schema({
    title: { type: String, required: true },
    completed: { type: Boolean, default: false },
});

module.exports = mongoose.model("Task", taskSchema);

Enter fullscreen mode Exit fullscreen mode
  1. Define API Endpoints:

Create, read, update, delete (CRUD) operations.

app.post("/tasks", async (req, res) => {
    const newTask = await Task.create(req.body);
    res.status(201).json(newTask);
});


Enter fullscreen mode Exit fullscreen mode

Day 3: Set Up the Front-End with React

Goals:

Build the structure for your React app.

Steps:

  1. Create React App:
npx create-react-app frontend
Enter fullscreen mode Exit fullscreen mode
  1. Setup Folder Structure:
frontend/
├── src/
    ├── components/
    ├── pages/
    ├── App.js
    ├── index.js

Enter fullscreen mode Exit fullscreen mode
  1. Build a Basic UI:

Use App.js to display a list of tasks and a form for adding new tasks.

  1. Install Axios:
npm install axios

Enter fullscreen mode Exit fullscreen mode

Day 4: Connect Front-End to Back-End

Goals:

Fetch tasks from the API and display them in the UI.

Steps:

  1. Fetch Tasks:
import axios from "axios";

const fetchTasks = async () => {
    const { data } = await axios.get("http://localhost:5000/tasks");
    setTasks(data);
};
Enter fullscreen mode Exit fullscreen mode
  1. Handle Form Submissions:

Post new tasks to the server using Axios.

  1. Test API Integration: Ensure you can fetch, create, and delete tasks from the UI.

Day 5: Add Interactivity

Goals:

Add features like task completion and editing.

Steps:

  1. Update Tasks:

Use a checkbox to mark tasks as completed.

const toggleTask = async (id) => {
    await axios.put(`http://localhost:5000/tasks/${id}`, { completed: true });
    fetchTasks();
};

Enter fullscreen mode Exit fullscreen mode
  1. Delete Tasks:

Add a delete button to each task.

  1. Polish the UI: Use CSS or libraries like Bootstrap to style the app.

Day 6: Deploy the App

Goals:

Deploy both the front-end and back-end.

Steps:

  1. Back-End Deployment:

Use Render, Heroku, or AWS to deploy your Node.js API.

  1. Front-End Deployment:

Deploy your React app using Vercel or Netlify.

  1. Update CORS Settings:

Allow requests from the deployed front-end URL.

Day 7: Test and Launch

Goals:

Test the application and fix any bugs.

Steps:

  1. Test all features (CRUD operations, responsiveness, etc.).

  2. Gather feedback from friends or colleagues.

  3. Celebrate your first full-stack app!

What’s Next?

Congratulations! You’ve built your first full-stack app with MERN. From here, you can:

Add authentication using JWT.

Implement advanced features like pagination and search.

Explore other stacks like MEAN or JAMstack.

What will you build next? Share your journey in the comments!

MERNStack #FullStackDevelopment #WebDevelopmentForBeginners #ReactJS #NodeJS #MongoDB

Top comments (0)