Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# node-mongodb-todo

Part 1 of the <a href="https://codedamn.com/learn/node-mongodb-fundamentals">Learn MongoDB with Node.js</a> 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:

<img src="frontend_todo.png" width="375" height="400" hspace="1cm"/> <img src="database.png" width="400" height="400"/>
4 changes: 3 additions & 1 deletion assets/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand Down
Binary file added database.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added frontend_todo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 10 additions & 6 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")));

Expand All @@ -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,
},
Expand All @@ -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);
Expand All @@ -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");
});