Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lecturn 1-10 Section 2 #199

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"editor.formatOnSave": true,
"prettier.requireConfig": true
}
96 changes: 96 additions & 0 deletions 1-node-farm/starter/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const fs = require('fs');
const http = require('http');
const url = require('url');

const slugify = require('slugify');

const replaceTemplate = require('./modules/replaceTemplate');

/////////////////////////////////////////
// FIles
/* // Blocking, synchronous way
const textIn = fs.readFileSync('./txt/input.txt', 'utf-8');
// console.log(textIn);
const textOut = `This is what we know about the avocado: ${textIn} on ${Date.now()}`;
fs.writeFileSync('./txt/output.txt',textOut);
console.log('File written!'); */



// Blocking, asynchronous way

// fs.readFile('./txt/startcds.txt', 'utf-8',(err , data1) => {
// if (err) return console.log('Error');
// fs.readFile(`./txt/${data1}.txt`, 'utf-8',(err , data2) => {
// console.log(data2);
// fs.readFile('./txt/append.txt', 'utf-8',(err , data3) => {
// console.log(data3);
// fs.writeFile('./txt/final.txt',`${data2}\n${data3}`, 'utf-8', err => {
// console.log('Your File has been written ')

// })
// });
// });
// });

// console.log(' Will read file!');



/////////////////////////////////////////
// SERVER

const tempOverview = fs.readFileSync(`${__dirname}/templates/template-overview.html`, 'utf-8');

const tempCard = fs.readFileSync(`${__dirname}/templates/template-card.html`, 'utf-8');

const tempProduct = fs.readFileSync(`${__dirname}/templates/template-product.html`, 'utf-8');

const data = fs.readFileSync(`${__dirname}/dev-data/data.json`, 'utf-8');

const datObj = JSON.parse(data);

const slugs = datObj.map(el => slugify(el.productName, {lower: true}));

const server = http.createServer((req, res) => {
const {query, pathname} = url.parse(req.url, true);

// OVERVIEW page
if(pathname === '/' || pathname === '/overview'){
res.writeHead(200, {'Content-type' : 'text/html'});

const cardsHtml = datObj.map(el => replaceTemplate(tempCard, el)).join('');
const Updated_tempOverview = tempOverview.replace('{%PRODUCT_CARDS%}', cardsHtml);
res.end(Updated_tempOverview);

// PRODUCT page
} else if (pathname === '/product'){
res.writeHead(200, {'Content-type' : 'text/html'});
const product = datObj[query.id]
const productHtml = replaceTemplate(tempProduct, product);

res.end(productHtml);

// API page
} else if (pathname === '/api') {
res.writeHead(200, {'Content-type' : 'application/json'});
res.end(data);

// NOT found
} else{
res.writeHead(404, {
'content-type': 'text/html',
'my-own-header': 'hello-world'
});
res.end('<h1>Page Not found!</h1>');
}
});



server.listen(8000,'localhost', () => {
console.log('Listening to requess on port 8000')
});



13 changes: 13 additions & 0 deletions 1-node-farm/starter/modules/replaceTemplate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = (temp, product) => {
let output = temp.replace(/{%PRODUCTNAME%}/g, product.productName);
output = output.replace(/{%IMAGE%}/g, product.image);
output = output.replace(/{%PRICE%}/g, product.price);
output = output.replace(/{%FROM%}/g, product.from);
output = output.replace(/{%NUTRIENTS%}/g, product.nutrients);
output = output.replace(/{%QUANTITY%}/g, product.quantity);
output = output.replace(/{%DESCRIPTION%}/g, product.description);
output = output.replace(/{%ID%}/g, product.id);

if(!product.organic) output = output.replace(/{%NOT_ORGANIC%}/g, 'not-organic');
return output;
}
Loading