├── README.md ├── dev-data └── data.json ├── index.js ├── templates ├── template-card.html ├── template-overview.html └── template-product.html └── txt ├── append.txt ├── final.txt ├── input.txt ├── output.txt ├── read-this.txt └── start.txt /README.md: -------------------------------------------------------------------------------- 1 | Project name:Node Farm
2 | 👇👇👇👇👇👇👇👇
3 | My Node Farm project is my first project in node js, in this project the data is taken from a json file and it works through an http server 4 | 5 |

Run the following commands to clone the project and install the required libraries:

6 | git clone: https://github.com/SardorbekCoder07/nodeJS_First_Project.git
7 | cd nodeJS_First_Project
8 | Download the project : npm install
9 | Open your terminal : node index.js
10 | Reminder!!!
11 | If you rename the index.js file, the node index.js command will not work
12 | 13 | 14 | -------------------------------------------------------------------------------- /dev-data/data.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": 0, 4 | "productName": "Fresh Avocados", 5 | "image": "🥑", 6 | "from": "Spain", 7 | "nutrients": "Vitamin B, Vitamin K", 8 | "quantity": "4 🥑", 9 | "price": "6.50", 10 | "organic": true, 11 | "description": "A ripe avocado yields to gentle pressure when held in the palm of the hand and squeezed. The fruit is not sweet, but distinctly and subtly flavored, with smooth texture. The avocado is popular in vegetarian cuisine as a substitute for meats in sandwiches and salads because of its high fat content. Generally, avocado is served raw, though some cultivars, including the common 'Hass', can be cooked for a short time without becoming bitter. It is used as the base for the Mexican dip known as guacamole, as well as a spread on corn tortillas or toast, served with spices." 12 | }, 13 | { 14 | "id": 1, 15 | "productName": "Goat and Sheep Cheese", 16 | "image": "🧀", 17 | "from": "Portugal", 18 | "nutrients": "Vitamin A, Calcium", 19 | "quantity": "250g", 20 | "price": "5.00", 21 | "organic": false, 22 | "description": "Creamy and distinct in flavor, goat cheese is a dairy product enjoyed around the world. Goat cheese comes in a wide variety of flavors and textures, from soft and spreadable fresh cheese to salty, crumbly aged cheese. Although it’s made using the same coagulation and separation process as cheese made from cow’s milk, goat cheese differs in nutrient content." 23 | }, 24 | { 25 | "id": 2, 26 | "productName": "Apollo Broccoli", 27 | "image": "🥦", 28 | "from": "Portugal", 29 | "nutrients": "Vitamin C, Vitamin K", 30 | "quantity": "3 🥦", 31 | "price": "5.50", 32 | "organic": true, 33 | "description": "Broccoli is known to be a hearty and tasty vegetable which is rich in dozens of nutrients. It is said to pack the most nutritional punch of any vegetable. When we think about green vegetables to include in our diet, broccoli is one of the foremost veggies to come to our mind. Broccoli is a cruciferous vegetable and part of the cabbage family, which includes vegetables such as Brussel sprouts and kale. Although the tastes are different, broccoli and these other vegetables are from the same family." 34 | }, 35 | { 36 | "id": 3, 37 | "productName": "Baby Carrots", 38 | "image": "🥕", 39 | "from": "France", 40 | "nutrients": "Vitamin A, Vitamin K", 41 | "quantity": "20 🥕", 42 | "price": "3.00", 43 | "organic": true, 44 | "description": "The carrot is a root vegetable that is often claimed to be the perfect health food. It is crunchy, tasty and highly nutritious. Carrots are a particularly good source of beta-carotene, fiber, vitamin K, potassium and antioxidants. Carrots have a number of health benefits. They are a weight loss friendly food and have been linked to lower cholesterol levels and improved eye health." 45 | }, 46 | { 47 | "id":4 , 48 | "productName": "Apple Carrots", 49 | "image": "🍎🍏", 50 | "from": "Uzbekistan", 51 | "nutrients": "Vitamin A, Vitamin K", 52 | "quantity": "20 🍎🍏", 53 | "price": "3.00", 54 | "organic": true, 55 | "description": "The carrot is a root vegetable that is often claimed to be the perfect health food. It is crunchy, tasty and highly nutritious. Carrots are a particularly good source of beta-carotene, fiber, vitamin K, potassium and antioxidants. Carrots have a number of health benefits. They are a weight loss friendly food and have been linked to lower cholesterol levels and improved eye health." 56 | }, 57 | { 58 | "id": 5, 59 | "productName": "Sweet Corncobs", 60 | "image": "🌽", 61 | "from": "Germany", 62 | "nutrients": "Vitamin C, Magnesium", 63 | "quantity": "2 🌽", 64 | "price": "2.00", 65 | "organic": false, 66 | "description": "Also known as maize, corn is one of the most popular cereal grains in the world. Popcorn and sweet corn are commonly eaten varieties, but refined corn products are also widely consumed, frequently as ingredients in foods. These include tortillas, tortilla chips, polenta, cornmeal, corn flour, corn syrup, and corn oil. Whole-grain corn is as healthy as any cereal grain, rich in fiber and many vitamins, minerals, and antioxidants." 67 | } 68 | ] 69 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const { log } = require('console'); 2 | const fs = require('fs'); 3 | const http = require('http'); 4 | const { Logger } = require('sass'); 5 | const URL = require('url') 6 | 7 | ////////////////////////////////////////////////////////////////// 8 | //FILES 9 | 10 | 11 | // const varib=fs.readFileSync('./txt/input.txt','utf-8') 12 | // const textOut=`This is what we know about the avacado: ${varib}.\nCreated on ${Date.now()}`; 13 | // fs.writeFileSync('./txt/output.txt',textOut) 14 | // console.log('file written') 15 | 16 | //This is section async code 17 | // fs.readFile('./txt/start.txt','utf-8',(err,data1)=>{ 18 | // fs.readFile(`./txt/${data1}.txt`,'utf-8',(err,data2)=>{ 19 | // console.log(data2) 20 | // fs.readFile('./txt/append.txt','utf-8',(err,data3)=>{ 21 | // console.log(data3); 22 | // fs.writeFile('./txt/final.txt',`Here is the text;${data2}\n${data3}`,'utf-8',err=>{ 23 | // console.log('your file has been written'); 24 | // }) 25 | // }) 26 | // }) 27 | // }) 28 | // console.log('will read file') 29 | ////////////////////////////////////////////////////////////////// 30 | // //SERVER 31 | // const server=http.createServer((req,res)=>{ 32 | // console.log(req); 33 | // res.end('Hello from the server') 34 | // }) 35 | // server.listen(8000,'127.0.0.1',()=>{ 36 | // console.log('listening on port 7000'); 37 | // }) 38 | const replaceTemplate = (temp, product) => { 39 | let output = temp.replace(/{%PRODUCTNAME%}/g, product.productName) 40 | output = output.replace(/{%IMAGE%}/g, product.image) 41 | output = output.replace(/{%PRICE%}/g, product.price) 42 | output = output.replace(/{%FROM%}/g, product.from) 43 | output = output.replace(/{%NUTRIENTS%}/g, product.nutrients) 44 | output = output.replace(/{%QUANTITY%}/g, product.quantity) 45 | output = output.replace(/{%DESCRIPTION%}/g, product.description) 46 | output = output.replace(/{%ID%}/g, product.id) 47 | 48 | if (!product.organic) output = output.replace(/{%NOT_ORGANIC%}/g, 'not-organic') 49 | 50 | return output 51 | } 52 | 53 | const tempOverview = fs.readFileSync(`${__dirname}/templates/template-overview.html`, 'utf-8') 54 | const tempСard = fs.readFileSync(`${__dirname}/templates/template-card.html`, 'utf-8') 55 | const tempProduct = fs.readFileSync(`${__dirname}/templates/template-product.html`, 'utf-8') 56 | const data = fs.readFileSync(`${__dirname}/dev-data/data.json`, 'utf-8') 57 | const dataObj = JSON.parse(data) 58 | 59 | const server = http.createServer((req, res) => { 60 | 61 | const { query, pathname } = URL.parse(req.url, true); 62 | 63 | //Overview page 64 | if (pathname === '/' || pathname === '/overview') { 65 | res.writeHead(200, { 'Content-type': 'text/html' }); 66 | 67 | const cardsHtml = dataObj.map(el => replaceTemplate(tempСard, el)).join('') 68 | const output = tempOverview.replace('{%PRODUCT_CARDS%}', cardsHtml) 69 | 70 | res.end(output); 71 | 72 | //Product page 73 | } else if (pathname === '/product') { 74 | res.writeHead(200, { 'Content-type': 'text/html' }); 75 | const product = dataObj[query.id] 76 | const output=replaceTemplate(tempProduct,product) 77 | res.end(output); 78 | 79 | //Overview page 80 | } else if (pathname === '/api') { 81 | res.writeHead(200, { 82 | 'Content-type': 'application/json', 83 | }); 84 | res.end(data); 85 | 86 | //Not found 87 | } else { 88 | res.writeHead(404, { 89 | 'Content-type': 'text/html', 90 | 'my-own-header': 'hello-world' 91 | }); 92 | res.end('

OOOO page not found

'); 93 | } 94 | }); 95 | 96 | server.listen(8000, '127.0.0.1', () => { 97 | console.log('Server is running on port 8000'); 98 | }); 99 | -------------------------------------------------------------------------------- /templates/template-card.html: -------------------------------------------------------------------------------- 1 |
2 |
{%IMAGE%}{%IMAGE%}
3 |
4 |

{%PRODUCTNAME%}

5 |
6 | 7 |
8 |
9 |
{%QUANTITY%} per 📦
10 |
11 | 12 |
13 |
{%PRICE%}€
14 |
15 |
16 | 17 | 18 | Detail 👉 19 | 20 |
-------------------------------------------------------------------------------- /templates/template-overview.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 11 | 15 | 16 | NODE FARM 17 | 18 | 188 | 189 | 190 | 191 |
192 |

🌽 Node Farm 🥦

193 | 194 |
195 | {%PRODUCT_CARDS%} 196 |
197 |
198 | 199 | 200 | -------------------------------------------------------------------------------- /templates/template-product.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 11 | 15 | 16 | {%PRODUCTNAME%} {%IMAGE%} /// NODE FARM 17 | 18 | 266 | 267 | 268 | 269 |
270 |

🌽 Node Farm 🥦

271 | 272 |
273 |
Organic
274 | 275 | 👈Back 276 | 277 |
278 | {%IMAGE%} 279 | {%IMAGE%} 280 | {%IMAGE%} 281 | {%IMAGE%} 282 | {%IMAGE%} 283 | {%IMAGE%} 284 | {%IMAGE%} 285 | {%IMAGE%} 286 | {%IMAGE%} 287 |
288 |

{%PRODUCTNAME%}

289 |
290 |

🌍From {%FROM%}

291 |

❤️ {%NUTRIENTS%}

292 |

📦 {%QUANTITY%}

293 |

🏷 {%PRICE%}€

294 |
295 | 296 | 297 | 🛒 298 | Add to shopping card {%PRICE%}€ 299 | 300 | 301 |

302 | {%DESCRIPTION%} 303 |

304 |
305 | 306 |
307 | 308 | 309 | -------------------------------------------------------------------------------- /txt/append.txt: -------------------------------------------------------------------------------- 1 | APPENDIX: Generally, avocados 🥑 are served raw, but some cultivars can be cooked for a short time without becoming bitter. -------------------------------------------------------------------------------- /txt/final.txt: -------------------------------------------------------------------------------- 1 | Here is the text;The avocado 🥑 is also used as the base for the Mexican dip known as guacamole, as well as a spread on corn tortillas or toast, served with spices. 2 | APPENDIX: Generally, avocados 🥑 are served raw, but some cultivars can be cooked for a short time without becoming bitter. -------------------------------------------------------------------------------- /txt/input.txt: -------------------------------------------------------------------------------- 1 | The avocado 🥑 is popular in vegetarian cuisine as a substitute for meats in sandwiches and salads because of its high fat content 😄 -------------------------------------------------------------------------------- /txt/output.txt: -------------------------------------------------------------------------------- 1 | This is what we know about the avacado: The avocado 🥑 is popular in vegetarian cuisine as a substitute for meats in sandwiches and salads because of its high fat content 😄. 2 | Created on 1721918359676 -------------------------------------------------------------------------------- /txt/read-this.txt: -------------------------------------------------------------------------------- 1 | The avocado 🥑 is also used as the base for the Mexican dip known as guacamole, as well as a spread on corn tortillas or toast, served with spices. -------------------------------------------------------------------------------- /txt/start.txt: -------------------------------------------------------------------------------- 1 | read-this --------------------------------------------------------------------------------