🔟 Final Mini Project

Simple Task Manager - Full Stack

Project: Task Manager

A simple full-stack app with:

Project Structure

📁 task-manager/ ├── 📁 backend/ │ ├── 📄 index.js │ └── 📄 package.json │ └── 📁 frontend/ ├── 📄 package.json └── 📁 src/ ├── 📄 App.jsx └── 📄 App.css

Backend Code

package.json

{
  "name": "task-backend",
  "scripts": { "start": "node index.js" },
  "dependencies": {
    "express": "^4.18.2",
    "mongoose": "^8.0.0",
    "cors": "^2.8.5"
  }
}

index.js

const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');

const app = express();
app.use(cors());
app.use(express.json());

// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/taskapp');

// Task Schema
const Task = mongoose.model('Task', {
  title: String,
  completed: { type: Boolean, default: false }
});

// GET all tasks
app.get('/api/tasks', async (req, res) => {
  const tasks = await Task.find();
  res.json(tasks);
});

// POST new task
app.post('/api/tasks', async (req, res) => {
  const task = await Task.create({ title: req.body.title });
  res.json(task);
});

// DELETE task
app.delete('/api/tasks/:id', async (req, res) => {
  await Task.findByIdAndDelete(req.params.id);
  res.json({ message: 'Deleted' });
});

// Toggle complete
app.patch('/api/tasks/:id/toggle', async (req, res) => {
  const task = await Task.findById(req.params.id);
  task.completed = !task.completed;
  await task.save();
  res.json(task);
});

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

Frontend Code (React with useState only)

App.jsx

import { useState } from 'react';
import './App.css';

function App() {
  const [tasks, setTasks] = useState([]);
  const [newTask, setNewTask] = useState('');
  const [loaded, setLoaded] = useState(false);

  // Load tasks
  async function loadTasks() {
    const res = await fetch('http://localhost:5000/api/tasks');
    const data = await res.json();
    setTasks(data);
    setLoaded(true);
  }

  // Add task
  async function addTask(e) {
    e.preventDefault();
    const res = await fetch('http://localhost:5000/api/tasks', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ title: newTask })
    });
    const task = await res.json();
    setTasks([...tasks, task]);
    setNewTask('');
  }

  // Delete task
  async function deleteTask(id) {
    await fetch(`http://localhost:5000/api/tasks/${id}`, { method: 'DELETE' });
    setTasks(tasks.filter(t => t._id !== id));
  }

  // Toggle complete
  async function toggleTask(id) {
    const res = await fetch(`http://localhost:5000/api/tasks/${id}/toggle`, { method: 'PATCH' });
    const updated = await res.json();
    setTasks(tasks.map(t => t._id === id ? updated : t));
  }

  return (
    <div className="app">
      <h1>📝 Task Manager</h1>
      
      {!loaded ? (
        <button onClick={loadTasks}>Load Tasks</button>
      ) : (
        <>
          <form onSubmit={addTask}>
            <input value={newTask} onChange={e => setNewTask(e.target.value)} />
            <button>Add</button>
          </form>
          <ul>
            {tasks.map(t => (
              <li key={t._id}>
                <input type="checkbox" checked={t.completed} onChange={() => toggleTask(t._id)} />
                <span style={{textDecoration: t.completed ? 'line-through' : 'none'}}>
                  {t.title}
                </span>
                <button onClick={() => deleteTask(t._id)}>🗑️</button>
              </li>
            ))}
          </ul>
        </>
      )}
    </div>
  );
}

export default App;

How to Run

1 Start MongoDB
2 Start Backend: cd backend && npm install && npm start
3 Start Frontend: cd frontend && npm install && npm run dev
🎉 Congratulations! You've completed the Node.js Tutorial!

You now know how to:
  • Build REST APIs with Express.js
  • Use async/await
  • Connect to MongoDB
  • Connect React to Node backend
  • Build full-stack applications!