Skip to content

Commit 1b6bd21

Browse files
committed
Improvements as the exercise got finalized
1 parent 1c275f6 commit 1b6bd21

3 files changed

Lines changed: 11 additions & 16 deletions

File tree

Lesson03/exercise_004/html/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ <h1 class="title">Welcome to Fresh Products Store!</h1>
1313
<div class="content">
1414
<a class="header">{{name}}</a>
1515
<div class="meta">
16-
<span>${{currency price}} / {{unit}}</span>
16+
<span>{{currency price}} / {{unit}}</span>
1717
</div>
1818
<div class="description">{{description}}</div>
1919
<div class="extra">

Lesson03/exercise_004/index.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,35 @@ const mime = require('mime');
55
const path = require('path');
66
const url = require('url');
77

8-
const staticDir = path.resolve('./static');
8+
const staticDir = path.resolve(`${__dirname}/static`);
99
console.log(`Static resources from ${staticDir}`);
1010

1111
const data = fs.readFileSync('products.json');
1212
const products = JSON.parse(data.toString());
1313
console.log(`Loaded ${products.length} products...`);
1414

15-
handlebars.registerHelper('currency', (number) => number.toFixed(2));
16-
17-
function handleNotFound(response) {
18-
response.writeHead(404);
19-
response.end();
20-
}
15+
handlebars.registerHelper('currency', (number) => `$${number.toFixed(2)}`);
2116

17+
const htmlTemplate = fs.readFileSync('html/index.html').toString();
2218
function handleProductsPage(requestUrl, response) {
23-
const template = handlebars.compile(fs.readFileSync('html/index.html').toString());
19+
const template = handlebars.compile(htmlTemplate);
2420

2521
response.writeHead(200);
26-
response.write(template({ products }));
22+
response.write(template({ products: products }));
2723
response.end();
2824
}
2925

3026
function handleStaticFile(pathname, response) {
3127
// For security reasons, only serve files from static directory
32-
const fullPath = path.resolve(`static${pathname}`);
33-
if (!fullPath.startsWith(staticDir)) {
34-
return handleNotFound(response);
35-
}
28+
const fullPath = path.join(staticDir, pathname);
3629

3730
// Check if file exists and is readable
3831
fs.access(fullPath, fs.constants.R_OK, (error) => {
3932
if (error) {
4033
console.error(`File is not readable: ${fullPath}`, error);
41-
return handleNotFound(response);
34+
response.writeHead(404);
35+
response.end();
36+
return;
4237
}
4338

4439
const contentType = mime.getType(path.extname(fullPath));

Lesson03/exercise_004/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "exercise_004",
33
"version": "1.0.0",
4-
"description": "",
4+
"description": "Exercise that demonstrate how to create a dynamic web server",
55
"main": "index.js",
66
"repository": {
77
"type": "git",

0 commit comments

Comments
 (0)