diff --git a/README.md b/README.md new file mode 100644 index 0000000..317fb66 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# node-mongodb-todo + +Part 1 of the Learn MongoDB with Node.js course from Codedamn. In this part a simple To Do app is created using Express. + +The installation and connection to a MongoDB database was the main issue address. A schema for this database was created using Mongoose. + +Additionally, a CRUD model was created to enable conversation between the frontend part of the application and the database. + +The pictures below depict the final working example: + + diff --git a/assets/code.js b/assets/code.js index 4fe6a07..3b98ef1 100644 --- a/assets/code.js +++ b/assets/code.js @@ -38,11 +38,13 @@ class item { } async edit(input) { + const oldInput = await input.value + // console.log(oldInput) const newInput = prompt("Enter new msg:", input.value); input.value = newInput; await fetch("/api/modify", { method: "POST", - body: JSON.stringify({ old: input.value, new: newInput }), + body: JSON.stringify({ old: oldInput, new: newInput }), headers: { "Content-Type": "application/json", }, diff --git a/database.png b/database.png new file mode 100644 index 0000000..ce0d0e1 Binary files /dev/null and b/database.png differ diff --git a/frontend_todo.png b/frontend_todo.png new file mode 100644 index 0000000..c797427 Binary files /dev/null and b/frontend_todo.png differ diff --git a/package.json b/package.json index f7f4587..3326a57 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,9 @@ "version": "1.0.0", "main": "index.js", "license": "MIT", + "scripts": { + "start": "nodemon script.js" + }, "dependencies": { "body-parser": "^1.19.0", "express": "^4.17.1", diff --git a/script.js b/script.js index adcb476..177fd9f 100644 --- a/script.js +++ b/script.js @@ -4,7 +4,7 @@ const app = express(); const mongoose = require("mongoose"); const Todo = require("./models/todo"); -mongoose.connect("mongodb://localhost/firstmongo"); +mongoose.connect("mongodb://localhost:27017/first_db"); app.use("/", express.static(path.resolve(__dirname, "assets"))); @@ -16,19 +16,20 @@ app.post("/api/delete", async (req, res) => { const response = await Todo.deleteOne({ record }); - console.log(response, "/api/delete repsonse"); + console.log(response, "/api/delete response"); res.json({ status: "ok" }); }); app.post("/api/modify", async (req, res) => { + console.log(req.body) const { old: oldTitle, new: newTitle } = req.body; const response = await Todo.updateOne( { record: oldTitle, }, - { + { // $set allows us to overwrite only the fields which are changin while preserving the other fields $set: { record: newTitle, }, @@ -40,12 +41,15 @@ app.post("/api/modify", async (req, res) => { res.json({ status: "ok" }); }); + +// reading from the DB app.get("/api/get", async (req, res) => { - const records = await Todo.find({}); - // console.log('Response => ', records) + const records = await Todo.find({}); // select all + console.log('Response => ', records) res.json(records); }); +// Adding items to DB app.post("/api/create", async (req, res) => { const record = req.body; console.log(record); @@ -59,5 +63,5 @@ app.post("/api/create", async (req, res) => { }); app.listen(13371, "127.0.0.1", () => { - console.log("Server up"); + console.log("Server up on http://localhost:13371"); });