a
This commit is contained in:
@@ -1,6 +1,3 @@
|
||||
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');
|
||||
|
||||
|
||||
92
index.js
Normal file
92
index.js
Normal file
@@ -0,0 +1,92 @@
|
||||
const { MongoClient, ServerApiVersion } = require('mongodb');
|
||||
const uri = 'mongodb+srv://ugochikay_db_user:bt06DpE6QZqLHZ1x>@cluster0.aqxmamw.mongodb.net/?appName=Cluster0';
|
||||
//Dependancies
|
||||
const express = require("express");
|
||||
const cors = require("cors"); //Change
|
||||
|
||||
const port = 3000;
|
||||
|
||||
const app = express();
|
||||
|
||||
//Middleware setup
|
||||
app.use(express.json());
|
||||
|
||||
app.use(cors("*")); //change
|
||||
|
||||
//API routes
|
||||
app.get("/get/example", async (req, res) =>{
|
||||
res.send("Hello this is a Message from Backend")
|
||||
})
|
||||
|
||||
const tasks = [
|
||||
{
|
||||
id: 1,
|
||||
completed: false,
|
||||
title:"Wash car",
|
||||
decription: "Make sure I have washed the car well",
|
||||
dueDate: "14/03/2026",
|
||||
dateCreated: new Date(Date.now()).toLocaleString()
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
completed: true,
|
||||
title: "Audit Report",
|
||||
description: "The Audit Report must be ready by due date",
|
||||
duedate: "16/03/2026",
|
||||
dateCreated: new Date(Date.now()).toLocaleString()
|
||||
},
|
||||
|
||||
{
|
||||
id: 3,
|
||||
completed: true,
|
||||
title: "Performance Report",
|
||||
description: "The Performance Report must be ready by due date",
|
||||
duedate: "16/03/2026",
|
||||
dateCreated: new Date(Date.now()).toLocaleString()
|
||||
},
|
||||
];
|
||||
|
||||
//Get all Tasks
|
||||
|
||||
app.get("/api/tasks", async (req, res) => {
|
||||
try {
|
||||
res.json(tasks);//Successful, responds to frontend with json list items
|
||||
|
||||
}
|
||||
catch (error){
|
||||
console.error(`Error: ${error}`);//Spits out the message in server console
|
||||
res.status(500).json({message:"Error grabbing tasks!"});//Responds with error 500 and a JSON mess
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
// Create a new Task and add it to the tasks array/list
|
||||
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);
|
||||
|
||||
}
|
||||
catch (error){
|
||||
console.error(`ERROR: ${error}`);
|
||||
res.status(500).json({message: "Error creating the task"});
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
//App startup
|
||||
app.listen(port, () => {
|
||||
console.log(`To Do App listening on port ${port}`);
|
||||
});
|
||||
Reference in New Issue
Block a user