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

Follow up #230

Open
wants to merge 3 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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${file}"
}
]
}
80 changes: 80 additions & 0 deletions 1-node-farm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const fs = require("fs");
const http = require("http");
const url = require("url");
const slugify = require("slugify");
const replaceProductTemplate = require("./starter/modules/replaceTemplate");

const OVERVIEW_ROUTE = "/overview";
const ROOT = "/";
const PRODUCT_ROUTE = "/product";
const API_ROUTE = "/api";

//here we used readFileAsync to load all products data and templates at the beginning
const productData = fs.readFileSync("./starter/dev-data/data.json", "utf-8");
const templateOverview = fs.readFileSync(
"./starter/templates/overview.html",
"utf-8"
);
const templateProduct = fs.readFileSync(
"./starter/templates/product.html",
"utf-8"
);
const templateProductCard = fs.readFileSync(
"./starter/templates/product-card.html",
"utf-8"
);

const jsonData = JSON.parse(productData);
const slugs = jsonData.map((product) =>
slugify(product.productName, { lowercase: true })
);

const server = http.createServer((request, response) => {
const { query, pathname } = url.parse(request.url, true);
switch (pathname) {
case ROOT:
case OVERVIEW_ROUTE:
displayAllProducts(response);
break;
case PRODUCT_ROUTE:
displayProductDetails(response, query.id);
break;
case API_ROUTE:
showJson(response);
break;
default:
response.end("no such page");
break;
}
});

function displayAllProducts(response) {
response.writeHead(200, { "Content-Type": "text/html" });
const cardsHtml = jsonData
.map((product) => replaceProductTemplate(templateProductCard, product))
//We joined the list with new line to get html content without list square brackets []
.join("\n");
const output = templateOverview.replace(/{%PRODUCT_CARDS%}/g, cardsHtml);
response.end(output);
}
function displayProductDetails(response, productId) {
console.log(`currnt product id: ${productId}`);
response.writeHead(200, { "Content-Type": "text/html" });
const detailHtml = jsonData[productId];
// console.log(detailHtml);
const output = replaceProductTemplate(templateProduct, detailHtml);
response.end(output);
}

function showJson(response) {
response.writeHead(200, { "Content-Type": "application/json" });
response.end(productData);
}

server.listen(8000, "127.0.0.1", () => {
console.log("listening on 8000");
});

// fs.readFile("./starter/txt/input.txt", "utf8", (error, data) => {
// console.log(data)
// });
16 changes: 16 additions & 0 deletions 1-node-farm/node_modules/.bin/nodemon

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions 1-node-farm/node_modules/.bin/nodemon.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions 1-node-farm/node_modules/.bin/nodemon.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions 1-node-farm/node_modules/.bin/nodetouch

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions 1-node-farm/node_modules/.bin/nodetouch.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions 1-node-farm/node_modules/.bin/nodetouch.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions 1-node-farm/node_modules/.bin/nopt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions 1-node-farm/node_modules/.bin/nopt.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions 1-node-farm/node_modules/.bin/nopt.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions 1-node-farm/node_modules/.bin/semver

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions 1-node-farm/node_modules/.bin/semver.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions 1-node-farm/node_modules/.bin/semver.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading