Skip to content

practice #7

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

Open
wants to merge 1 commit into
base: node-2
Choose a base branch
from
Open
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
74 changes: 25 additions & 49 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,31 @@
const http = require('http');
const fs = require('fs');

const index = fs.readFileSync('index.html', 'utf-8');
const data = JSON.parse(fs.readFileSync('data.json', 'utf-8'));
const products = data.products;

const server = http.createServer((req, res) => {
console.log(req.url, req.method);

if(req.url.startsWith('/product')){
const id = req.url.split('/')[2]
const product = products.find(p=>p.id===(+id))
console.log(product)
res.setHeader('Content-Type', 'text/html');
let modifiedIndex = index.replace('**title**', product.title)
.replace('**url**', product.thumbnail)
.replace('**price**', product.price)
.replace('**rating**', product.rating)
res.end(modifiedIndex);
return;
}
// '/product':
// res.setHeader('Content-Type', 'text/html');
// let modifiedIndex = index.replace('**title**', product.title)
// .replace('**url**', product.thumbnail)
// .replace('**price**', product.price)
// .replace('**rating**', product.rating)
// res.end(modifiedIndex);
// break;



switch (req.url) {
case '/':
res.setHeader('Content-Type', 'text/html');
res.end(index);
break;
case '/api':
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(data));
break;

default:
res.writeHead(404);
res.end();
}

console.log('server started ');
// res.setHeader('Dummy', 'DummyValue');

//
});

server.listen(8080);


const express = require('express');
const server = express();

server.use(express.static('public'))
server.use(express.json())

server.get('/products', (req, res) => {
res.json(products)
})

server.get('/products', (req, res) => {
res.json(products)
})

server.post('/products', (req, res) => {
products.push(req.body);
res.json(products);

})

server.listen(8081, () => {
console.log('server started')
});
586 changes: 586 additions & 0 deletions package-lock.json
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -8,5 +8,8 @@
},
"keywords": [],
"author": "",
"license": "ISC"
"license": "ISC",
"dependencies": {
"express": "^4.18.2"
}
}
256 changes: 256 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
/>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"
/>
<style>
body {
background-color: #eee;
}
.container {
width: 900px;
}

.card {
background-color: #fff;
border: none;
border-radius: 10px;
width: 250px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.image-container {
position: relative;
}

.thumbnail-image {
border-radius: 10px !important;
}

.discount {
background-color: red;
padding-top: 1px;
padding-bottom: 1px;
padding-left: 4px;
padding-right: 4px;
font-size: 10px;
border-radius: 6px;
color: #fff;
}

.wishlist {
height: 25px;
width: 25px;
background-color: #eee;
display: flex;
justify-content: center;
align-items: center;
border-radius: 50%;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.first {
position: absolute;
width: 100%;
padding: 9px;
}

.dress-name {
font-size: 13px;
font-weight: bold;
width: 75%;
}

.new-price {
font-size: 13px;
font-weight: bold;
color: red;
}
.old-price {
font-size: 8px;
font-weight: bold;
color: grey;
}

.btn {
width: 14px;
height: 14px;
border-radius: 50%;
padding: 3px;
}

.creme {
background-color: #fff;
border: 2px solid grey;
}
.creme:hover {
border: 3px solid grey;
}

.creme:focus {
background-color: grey;
}

.red {
background-color: #fff;
border: 2px solid red;
}

.red:hover {
border: 3px solid red;
}
.red:focus {
background-color: red;
}

.blue {
background-color: #fff;
border: 2px solid #40c4ff;
}

.blue:hover {
border: 3px solid #40c4ff;
}
.blue:focus {
background-color: #40c4ff;
}
.darkblue {
background-color: #fff;
border: 2px solid #01579b;
}
.darkblue:hover {
border: 3px solid #01579b;
}
.darkblue:focus {
background-color: #01579b;
}
.yellow {
background-color: #fff;
border: 2px solid #ffca28;
}
.yellow:hover {
border-radius: 3px solid #ffca28;
}
.yellow:focus {
background-color: #ffca28;
}

.item-size {
width: 15px;
height: 15px;
border-radius: 50%;
background: #fff;
border: 1px solid grey;
color: grey;
font-size: 10px;
text-align: center;
align-items: center;
display: flex;
justify-content: center;
}

.rating-star {
font-size: 10px !important;
}
.rating-number {
font-size: 10px;
color: grey;
}
.buy {
font-size: 12px;
color: purple;
font-weight: 500;
}

.voutchers {
background-color: #fff;
border: none;
border-radius: 10px;
width: 190px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
overflow: hidden;
}
.voutcher-divider {
display: flex;
}
.voutcher-left {
width: 60%;
}
.voutcher-name {
color: grey;
font-size: 9px;
font-weight: 500;
}
.voutcher-code {
color: red;
font-size: 11px;
font-weight: bold;
}
.voutcher-right {
width: 40%;
background-color: purple;
color: #fff;
}

.discount-percent {
font-size: 12px;
font-weight: bold;
position: relative;
top: 5px;
}
.off {
font-size: 14px;
position: relative;
bottom: 5px;
}
</style>

<div class="container mt-5">
<div class="row">
<div class="col-md-3">
<div class="card">
<div class="image-container">
<div class="first">
<div class="d-flex justify-content-between align-items-center">
<span class="discount">-25%</span>
<span class="wishlist"><i class="fa fa-heart-o"></i></span>
</div>
</div>

<img
src="**url**"
class="img-fluid rounded thumbnail-image"
/>
</div>

<div class="product-detail-container p-2">
<div class="d-flex justify-content-between align-items-center">
<h5 class="dress-name">**title**</h5>

<div class="d-flex flex-column mb-2">
<span class="new-price">&#8377 **price**</span>
<small class="old-price text-right">&#8377 700</small>
</div>
</div>



<div class="d-flex justify-content-between align-items-center pt-1">
<div>
<i class="fa fa-star-o rating-star"></i>
<span class="rating-number">**rating**</span>
</div>

<span class="buy">BUY +</span>
</div>
</div>
</div>


</div>

</div>
</div>