From 48f52e3ffca09f75730d76f89a0ead52cab4c94a Mon Sep 17 00:00:00 2001 From: mryunt02 Date: Sun, 3 Mar 2024 14:56:39 +0300 Subject: [PATCH 1/7] node lesson1-modules-file system --- 1-node-farm/starter/index.js | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 1-node-farm/starter/index.js diff --git a/1-node-farm/starter/index.js b/1-node-farm/starter/index.js new file mode 100644 index 0000000000..7f7701c4c8 --- /dev/null +++ b/1-node-farm/starter/index.js @@ -0,0 +1,3 @@ +const fs = require("fs"); +const hello = "hello world"; +console.log(fs.readFileSync("final")); From 1346d2bdb95dbe0950fb639ae44f6c5eb87e48f4 Mon Sep 17 00:00:00 2001 From: mryunt02 Date: Sun, 3 Mar 2024 15:31:14 +0300 Subject: [PATCH 2/7] node lesson2-writeFileSync --- 1-node-farm/starter/index.js | 7 ++++++- 1-node-farm/starter/txt/output.txt | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 1-node-farm/starter/txt/output.txt diff --git a/1-node-farm/starter/index.js b/1-node-farm/starter/index.js index 7f7701c4c8..03e00db679 100644 --- a/1-node-farm/starter/index.js +++ b/1-node-farm/starter/index.js @@ -1,3 +1,8 @@ const fs = require("fs"); const hello = "hello world"; -console.log(fs.readFileSync("final")); +const textIn = fs.readFileSync("./txt/input.txt", "utf-8"); // readFileSync("./path","") takes two arguments, first one is reading file we want,second is character encoding +console.log(textIn); +const textOut = `About avocado ${textIn}.\n Created on ${Date.now()}`; +fs.writeFileSync("./txt/output.txt", textOut); // writeFileSync("./path","text") takes two arguments, first one is the path to the file where the data will be written, second is the data that will be written to the file. if first data doesn't exist writeFileSync create a new file. +const readTextOut = fs.readFileSync("./txt/output.txt", "utf-8"); +console.log(readTextOut); diff --git a/1-node-farm/starter/txt/output.txt b/1-node-farm/starter/txt/output.txt new file mode 100644 index 0000000000..32adf3cd1f --- /dev/null +++ b/1-node-farm/starter/txt/output.txt @@ -0,0 +1,2 @@ +About avocado The avocado 🥑 is popular in vegetarian cuisine as a substitute for meats in sandwiches and salads because of its high fat content 😄. + Created on 1709468674591 \ No newline at end of file From ff2ef2a1357e762b02fe3a65473bc3a516b7fc6e Mon Sep 17 00:00:00 2001 From: mryunt02 Date: Sun, 3 Mar 2024 18:34:33 +0300 Subject: [PATCH 3/7] Reading and Writing Files Asynchronously --- 1-node-farm/starter/index.js | 30 +++++++++++++++++++++++------- 1-node-farm/starter/txt/output.txt | 4 ++-- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/1-node-farm/starter/index.js b/1-node-farm/starter/index.js index 03e00db679..16e6f3312b 100644 --- a/1-node-farm/starter/index.js +++ b/1-node-farm/starter/index.js @@ -1,8 +1,24 @@ const fs = require("fs"); -const hello = "hello world"; -const textIn = fs.readFileSync("./txt/input.txt", "utf-8"); // readFileSync("./path","") takes two arguments, first one is reading file we want,second is character encoding -console.log(textIn); -const textOut = `About avocado ${textIn}.\n Created on ${Date.now()}`; -fs.writeFileSync("./txt/output.txt", textOut); // writeFileSync("./path","text") takes two arguments, first one is the path to the file where the data will be written, second is the data that will be written to the file. if first data doesn't exist writeFileSync create a new file. -const readTextOut = fs.readFileSync("./txt/output.txt", "utf-8"); -console.log(readTextOut); +// const hello = "hello world"; +// // const textIn = fs.readFileSync("./txt/input.txt", "utf-8"); // readFileSync("./path","") takes two arguments, first one is reading file we want,second is character encoding +// //BLOCKING SYNCHRONOUS CODES LINES 4-10 +// console.log(textIn); +// const textOut = `About avocado ${textIn}.\n Created on ${Date.now()}`; +// fs.writeFileSync("./txt/output.txt", textOut); // writeFileSync("./path","text") takes two arguments, first one is the path to the file where the data will be written, second is the data that will be written to the file. if first data doesn't exist writeFileSync create a new file. +// const readTextOut = fs.readFileSync("./txt/output.txt", "utf-8"); +// console.log(readTextOut); + +// NON-BLOCKING ASYNCHRONOUS CODES +fs.readFile("./txt/start.txt", "utf-8", (err, data1) => { + console.log(data1); // the output will be read-this + fs.readFile(`./txt/${data1}.txt`, "utf-8", (err, data2) => { + console.log(data2); // the output will be the content of read-this.txt + fs.readFile("./txt/append.txt", "utf-8", (err, data3) => { + console.log(data3); // the output will be the content of append.txt + fs.writeFile("./txt/final.txt", `${data2}\n${data3}`, "utf-8", (err) => { + console.log("Your file has been written"); // content of final.txt will be data2(content of read-this.txt)+data3(content of append.txt) + }); + }); + }); +}); +console.log("will read file..."); diff --git a/1-node-farm/starter/txt/output.txt b/1-node-farm/starter/txt/output.txt index 32adf3cd1f..ca9517a202 100644 --- a/1-node-farm/starter/txt/output.txt +++ b/1-node-farm/starter/txt/output.txt @@ -1,2 +1,2 @@ -About avocado The avocado 🥑 is popular in vegetarian cuisine as a substitute for meats in sandwiches and salads because of its high fat content 😄. - Created on 1709468674591 \ No newline at end of file +About avocado undefined. + Created on 1709477832155 \ No newline at end of file From 005615ab450923aca88be5b8a2a4277966c7bc5a Mon Sep 17 00:00:00 2001 From: mryunt02 Date: Sun, 3 Mar 2024 21:56:24 +0300 Subject: [PATCH 4/7] Creating a simple web server with http module --- 1-node-farm/starter/index.js | 40 ++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/1-node-farm/starter/index.js b/1-node-farm/starter/index.js index 16e6f3312b..ddb758ee96 100644 --- a/1-node-farm/starter/index.js +++ b/1-node-farm/starter/index.js @@ -1,4 +1,7 @@ const fs = require("fs"); +const http = require("http"); +/////////////////////// +////////// FILES // const hello = "hello world"; // // const textIn = fs.readFileSync("./txt/input.txt", "utf-8"); // readFileSync("./path","") takes two arguments, first one is reading file we want,second is character encoding // //BLOCKING SYNCHRONOUS CODES LINES 4-10 @@ -9,16 +12,27 @@ const fs = require("fs"); // console.log(readTextOut); // NON-BLOCKING ASYNCHRONOUS CODES -fs.readFile("./txt/start.txt", "utf-8", (err, data1) => { - console.log(data1); // the output will be read-this - fs.readFile(`./txt/${data1}.txt`, "utf-8", (err, data2) => { - console.log(data2); // the output will be the content of read-this.txt - fs.readFile("./txt/append.txt", "utf-8", (err, data3) => { - console.log(data3); // the output will be the content of append.txt - fs.writeFile("./txt/final.txt", `${data2}\n${data3}`, "utf-8", (err) => { - console.log("Your file has been written"); // content of final.txt will be data2(content of read-this.txt)+data3(content of append.txt) - }); - }); - }); -}); -console.log("will read file..."); +// fs.readFile("./txt/start.txt", "utf-8", (err, data1) => { +// console.log(data1); // the output will be read-this +// fs.readFile(`./txt/${data1}.txt`, "utf-8", (err, data2) => { +// console.log(data2); // the output will be the content of read-this.txt +// fs.readFile("./txt/append.txt", "utf-8", (err, data3) => { +// console.log(data3); // the output will be the content of append.txt +// fs.writeFile("./txt/final.txt", `${data2}\n${data3}`, "utf-8", (err) => { +// console.log("Your file has been written"); // content of final.txt will be data2(content of read-this.txt)+data3(content of append.txt) +// }); +// }); +// }); +// }); +// console.log("will read file..."); + +/////////////////////// +////////// SERVER + +const server = http.createServer((req, res) => { + res.end("Hello from the server!"); // when a request is received, the server will respond with the message +}); // create an HTTP server + +server.listen(8000, "127.0.0.1", () => { + console.log("Listening to requests on port 8000"); // callback function runs when listening start +}); //will start listening for incoming requests(starting up the server) From 5b04b66a54bf0e70ff77c5decc6ffdaef153e467 Mon Sep 17 00:00:00 2001 From: mryunt02 Date: Sun, 3 Mar 2024 22:44:47 +0300 Subject: [PATCH 5/7] Routing --- 1-node-farm/starter/index.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/1-node-farm/starter/index.js b/1-node-farm/starter/index.js index ddb758ee96..7a4093b34a 100644 --- a/1-node-farm/starter/index.js +++ b/1-node-farm/starter/index.js @@ -1,5 +1,6 @@ const fs = require("fs"); const http = require("http"); +const url = require("url"); /////////////////////// ////////// FILES // const hello = "hello world"; @@ -30,7 +31,20 @@ const http = require("http"); ////////// SERVER const server = http.createServer((req, res) => { - res.end("Hello from the server!"); // when a request is received, the server will respond with the message + console.log(req.url); + const pathName = req.url; + if (pathName === "/" || pathName === "/overview") { + res.end("this is the overview"); + } else if (pathName === "/product") { + res.end("this is the product"); + } else { + res.writeHead(404, { + "Content-type": "text/html", //browser expecting some HTML + "my-own-header": "hello-world", //Created my own header + }); + res.end("

Page not found...

"); + } + //res.end("Hello from the server!"); // when a request is received, the server will respond with the message }); // create an HTTP server server.listen(8000, "127.0.0.1", () => { From dc296962e820e80309593cadc64ee43397054074 Mon Sep 17 00:00:00 2001 From: mryunt02 Date: Mon, 4 Mar 2024 00:32:18 +0300 Subject: [PATCH 6/7] Building a simple API --- 1-node-farm/starter/index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/1-node-farm/starter/index.js b/1-node-farm/starter/index.js index 7a4093b34a..fad876a533 100644 --- a/1-node-farm/starter/index.js +++ b/1-node-farm/starter/index.js @@ -29,6 +29,8 @@ const url = require("url"); /////////////////////// ////////// SERVER +const data = fs.readFileSync(`${__dirname}/dev-data/data.json`, "utf-8"); +const dataObj = JSON.parse(data); const server = http.createServer((req, res) => { console.log(req.url); @@ -37,6 +39,9 @@ const server = http.createServer((req, res) => { res.end("this is the overview"); } else if (pathName === "/product") { res.end("this is the product"); + } else if (pathName === "/api") { + res.writeHead(200, { "Content-type": "application/json" }); //for JSON format + res.end(data); } else { res.writeHead(404, { "Content-type": "text/html", //browser expecting some HTML From cdfc5038588b0e34ab60038a4320c4249667ceab Mon Sep 17 00:00:00 2001 From: mryunt02 Date: Mon, 4 Mar 2024 17:48:11 +0300 Subject: [PATCH 7/7] node.js basic project --- 1-node-farm/starter/index.js | 46 +++++-- .../starter/templates/template-card.html | 24 ++++ .../{overview.html => template-overview.html} | 118 +----------------- .../{product.html => template-product.html} | 46 +++---- 4 files changed, 83 insertions(+), 151 deletions(-) create mode 100644 1-node-farm/starter/templates/template-card.html rename 1-node-farm/starter/templates/{overview.html => template-overview.html} (54%) rename 1-node-farm/starter/templates/{product.html => template-product.html} (77%) diff --git a/1-node-farm/starter/index.js b/1-node-farm/starter/index.js index fad876a533..dbed29d126 100644 --- a/1-node-farm/starter/index.js +++ b/1-node-farm/starter/index.js @@ -29,17 +29,49 @@ const url = require("url"); /////////////////////// ////////// SERVER +const replaceTemplate = (temp, product) => { + let output = temp + .replace(/{%PRODUCTNAME%}/g, product.productName) + .replace(/{%IMAGE%}/g, product.image) + .replace(/{%FROM%}/g, product.from) + .replace(/{%NUTRIENTS%}/g, product.nutrients) + .replace(/{%QUANTITY%}/g, product.quantity) + .replace(/{%PRICE%}/g, product.price); + if (!product.organic) + output = output.replace(/{%NOT_ORGANIC%}/g, "not-organic"); + output = output + .replace(/{%ID%}/g, product.id) + .replace(/{%DESCRIPTION%}/g, product.description); + return output; +}; const data = fs.readFileSync(`${__dirname}/dev-data/data.json`, "utf-8"); const dataObj = JSON.parse(data); +const tempCard = fs.readFileSync("./templates/template-card.html", "utf-8"); +const tempProduct = fs.readFileSync( + "./templates/template-product.html", + "utf-8" +); +const tempOverview = fs.readFileSync( + "./templates/template-overview.html", + "utf-8" +); const server = http.createServer((req, res) => { - console.log(req.url); - const pathName = req.url; - if (pathName === "/" || pathName === "/overview") { - res.end("this is the overview"); - } else if (pathName === "/product") { - res.end("this is the product"); - } else if (pathName === "/api") { + const { query, pathname } = url.parse(req.url, true); + + if (pathname === "/" || pathname === "/overview") { + res.writeHead(200, { "Content-type": "text/html" }); + const cardsHtml = dataObj + .map((el) => replaceTemplate(tempCard, el)) + .join(""); + const output = tempOverview.replace("{%PRODUCT_CARDS%}", cardsHtml); + res.end(output); + } else if (pathname === "/product") { + res.writeHead(200, { "Content-type": "text/html" }); + const product = dataObj[query.id]; + const output = replaceTemplate(tempProduct, product); + res.end(output); + } else if (pathname === "/api") { res.writeHead(200, { "Content-type": "application/json" }); //for JSON format res.end(data); } else { diff --git a/1-node-farm/starter/templates/template-card.html b/1-node-farm/starter/templates/template-card.html new file mode 100644 index 0000000000..a109c12ebc --- /dev/null +++ b/1-node-farm/starter/templates/template-card.html @@ -0,0 +1,24 @@ +
+
{%IMAGE%}
+
+

{%PRODUCTNAME%}

+
+ +
+
+
Organic!
+
+ +
+
{%QUANTITY%} per 📦
+
+ +
+
{%PRICE%}€
+
+
+ + + Detail 👉 + +
diff --git a/1-node-farm/starter/templates/overview.html b/1-node-farm/starter/templates/template-overview.html similarity index 54% rename from 1-node-farm/starter/templates/overview.html rename to 1-node-farm/starter/templates/template-overview.html index d1934e7a52..6ccd334ea9 100644 --- a/1-node-farm/starter/templates/overview.html +++ b/1-node-farm/starter/templates/template-overview.html @@ -192,123 +192,7 @@

🌽 Node Farm 🥦

-
-
🥑🥑
- -
-

Fresh Avocado

-
- -
-
-
Organic!
-
- -
-
4 🥑 per 📦
-
- -
-
6.50€
-
-
- - - Detail 👉 - -
- -
-
🧀🧀
-
-

Goat and Sheep Cheese

-
- -
-
-
250g per 📦
-
- -
-
5.00€
-
-
- - - Detail 👉 - -
- -
-
🥦🥦
-
-

Apollo Broccoli

-
- -
-
-
Organic!
-
- -
-
3 🥦 per 📦
-
- -
-
5.50€
-
-
- - - Detail 👉 - -
- -
-
🥕🥕
-
-

Baby Carrots

-
- -
-
-
Organic!
-
- -
-
20 🥕 per 📦
-
- -
-
3.00€
-
-
- - - Detail 👉 - -
- -
-
🌽🌽
-
-

Sweet Corncobs

-
- -
-
-
2 🌽 per 📦
-
- -
-
2.00€
-
-
- - - Detail 👉 - -
+ {%PRODUCT_CARDS%}
diff --git a/1-node-farm/starter/templates/product.html b/1-node-farm/starter/templates/template-product.html similarity index 77% rename from 1-node-farm/starter/templates/product.html rename to 1-node-farm/starter/templates/template-product.html index 8b05bf5c9e..60fa73d3c1 100644 --- a/1-node-farm/starter/templates/product.html +++ b/1-node-farm/starter/templates/template-product.html @@ -13,7 +13,7 @@ href="https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/240/apple/155/ear-of-maize_1f33d.png" /> - Fresh Avocados 🥑 /// NODE FARM + {%PRODUCTNAME%} {%IMAGE%} /// NODE FARM