Files
diploma_todoapp/backend/index.js
2026-03-31 18:30:49 +08:00

92 lines
2.3 KiB
JavaScript

const { MongoClient, ServerApiVersion } = require('mongodb');
const uri = "mongodb+srv://<db_username>:<db_password>@cluster0.dombm2a.mongodb.net/?appName=Cluster0";
const express = require('express');
const cors = require('cors');
const port = 3001;
const app = express();
app.use(express.json());
app.use(cors("*"));
app.get("/test", async (req, res) => {
res.send("Hello World!");
});
const tasks = [
{
id: 1,
completed: false,
title: "Buy groceries for the week",
description: "Pick up vegetables, fruits, milk, eggs, and bread from the supermarket.",
dueDate: "14/03/2026",
dateCreated: new Date(Date.now()).toLocaleDateString("en-AU")
},
{
id: 2,
completed: false,
title: "Clean the apartment",
description: "Vacuum the floors, wipe kitchen surfaces, and take out the trash.",
dueDate: "16/03/2026",
dateCreated: new Date(Date.now()).toLocaleDateString("en-AU")
},
{
id: 3,
completed: false,
title: "Call the dentist for appointment",
description: "Book a routine checkup and confirm available time slots for next week.",
dueDate: "17/03/2026",
dateCreated: new Date(Date.now()).toLocaleDateString("en-AU")
},
{
id: 4,
completed: true,
title: "Do laundry and fold clothes",
description: "Wash dark and light loads separately, then fold and organize clean clothes.",
dueDate: "21/03/2026",
dateCreated: new Date(Date.now()).toLocaleDateString("en-AU")
}
];
app.get("/api/tasks", async (req, res) => {
try {
res.json(tasks);
}
catch (error) {
console.error(`Error: ${error}`)
res.status(500).json({ message: "Error getting tasks" })
}
});
app.post("/api/tasks/todo", async (req, res) => {
try {
const { title, description, dueDate } = req.body;
const newTask = {
id: tasks.length + 1,
completed: false,
title, title,
description, description,
dueDate, dueDate,
dateCreated: new Date(Date.now()).toLocaleDateString("en-AU"),
};
tasks.push(newTask);
res.json(newTask);
res.status(201).json(newTask);
}
catch (error) {
console.error(`Error: ${error}`)
res.status(500).json({ message: "Error creating task" })
}
});
app.listen(port, () => {
console.log(`To Do App listening on port ${port}`);
});