Back to Articles
Development

Building a Full-Stack Web Application

A step-by-step guide to building a simple full-stack To-Do App using React, Node.js, Express, and MongoDB. It covers backend setup, database integration, frontend development, API communication, and deployment, giving developers a practical introduction to connecting frontend and backend technologies in a complete web application.

Jorris NyangeAugust 25, 20264 min read
Building a Full-Stack Web Application

In today's fast-paced web development landscape, understanding both frontend and backend integration is crucial for developers. A full-stack project demonstrates the seamless connection between user interfaces and server-side logic, ensuring smooth data handling and user experience.

In this blog, we'll guide you through building a simple full-stack To-Do App using React (frontend), Node.js/Express (backend), and MongoDB (database). By the end, you'll have a fully functional web application that allows users to create, read, update, and delete tasks.


2. Project Overview

image
image

We’ll build a To-Do App with the following features:

Frontend (React)

  • User interface to add, view, update, and delete tasks.
  • API integration to fetch and manage tasks.

Backend (Node.js/Express)

  • REST API for CRUD operations.
  • Handles data requests and responses.

Database (MongoDB)

  • Stores tasks persistently.
  • Uses Mongoose for database interaction.

3. Setting Up the Backend

Step 1: Initialize the Project

hljs bash
mkdir backend && cd backend npm init -y

Step 2: Install Dependencies

hljs bash
npm install express mongoose cors dotenv

Step 3: Create the Server

Create server.js and set up a basic Express server:

hljs javascript
const express = require('express'); const mongoose = require('mongoose'); const cors = require('cors'); require('dotenv').config(); const app = express(); app.use(cors()); app.use(express.json()); mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true, }); const taskSchema = new mongoose.Schema({ title: String, completed: Boolean, }); const Task = mongoose.model('Task', taskSchema); app.get('/api/tasks', async (req, res) => { const tasks = await Task.find(); res.json(tasks); }); app.post('/api/tasks', async (req, res) => { const task = new Task(req.body); await task.save(); res.json(task); }); app.listen(5000, () => console.log('Server running on port 5000'));

Step 4: Connect to MongoDB

Create a .env file:

hljs plaintext
MONGO_URI=mongodb://localhost:27017/todoapp

Testing the API in Postman

image
image


4. Setting Up the Frontend

Step 1: Initialize the React Project

hljs bash
npx create-react-app frontend cd frontend npm install axios

Step 2: Create Components

TaskList.js
hljs javascript
import React, { useEffect, useState } from 'react'; import axios from 'axios'; function TaskList() { const [tasks, setTasks] = useState([]); useEffect(() => { axios.get('http://localhost:5000/api/tasks') .then(response => setTasks(response.data)); }, []); return ( <ul> {tasks.map(task => ( <li key={task._id}>{task.title}</li> ))} </ul> ); } export default TaskList;

5. Connecting Frontend and Backend

Enable CORS in Backend

Ensure cors is used in server.js:

hljs javascript
const cors = require('cors'); app.use(cors());

Fetch Data from Backend in React

hljs javascript
useEffect(() => { axios.get('http://localhost:5000/api/tasks') .then(response => setTasks(response.data)); }, []);

6. Deployment

Deploy the Backend

Use Render, Heroku, or Vercel to deploy the Node.js server.

Deploy the Frontend

Use Vercel or Netlify to deploy the React app.


7. Conclusion

We successfully built a full-stack To-Do App, covering backend setup, frontend UI, and full integration. This knowledge is essential for full-stack developers, enabling them to build and deploy complete applications.


Share this article

Found this helpful? Share it with your network.

Newsletter

Enjoyed This Article?

Subscribe to get more insights like this delivered straight to your inbox. No spam, unsubscribe anytime.

By subscribing, you agree to our Privacy Policy.