-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
53 lines (44 loc) · 1.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict';
const http = require('http');
const user = require('./user');
const products = require('./products');
function getShowcase(user, products) {
return products.map((product) => {
const showcaseItem = {
name: product.name
};
if (product.sale) {
showcaseItem.price = product.price * (100 - product.sale) / 100;
} else {
showcaseItem.price = product.price;
}
return showcaseItem;
});
}
function getBonuses(user, showcase) {
const random = Math.floor(Math.random() * showcase.length);
const randomProduct = showcase[random];
return [randomProduct];
}
http.createServer((req,res) => {
const showcase = getShowcase(user, products);
const bonuses = getBonuses(user, showcase);
const pageData = {showcase, bonuses};
res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
res.end(`<!DOCTYPE html>
<html>
<head>
<title>Витрина</title>
</head>
<body>
<h1>Витрина</h1>
<ul>
<li><strong>Товар 1</strong> - 100 ₽</li>
</ul>
<span>После покупки вам будет доступен бонус.</span>
<p>Витрина доступна только для зарегистрированных пользователей.</p>
</body>
</html>`);
}).listen(3000, () => {
console.log('Ready on http://localhost:3000');
});