-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex-utils.js
151 lines (134 loc) · 4.61 KB
/
index-utils.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const fs = require("fs")
const zlib = require("zlib");
const path = require("path");
const fsPromises = require("fs/promises");
const MIMES = require("./utils/MIMETypes.js");
const { renderPage } = require("./utils/serverRender.js");
//////////////////////////////////////////////////////////////////////////
function readFileAndRespond(filePath, response, statusCode=null) {
fs.stat(filePath, (error, stats) => {
if (error) {
console.log(error)
handleError(error, response)
} else {
const mime = MIMES.findMIMETypeFromExtension(path.extname(filePath));
if (mime.split("/")[0] == "image") {
// images should not be compressed
response.writeHead(statusCode || 200, {
"content-type": mime,
"cache-control": "max-age=2419200"
})
fs.createReadStream(filePath)
.pipe(response)
} else {
response.writeHead(statusCode || 200, {
"content-type": mime,
"content-encoding": "gzip",
"cache-control": "max-age=1209600"
})
fs.createReadStream(filePath)
.pipe(zlib.createGzip())
.pipe(response)
}
}
})
}
function handleError(error, response) {
if (error.code == "ENOENT") {
const filePath = path.join(__dirname, "frontend/html/error.html")
const errorCode = 404;
readFileAndRespond(filePath, response, errorCode)
} else {
console.log(error);
const filePath = path.join(__dirname, "frontend/html/error.html")
const errorCode = 404;
readFileAndRespond(filePath, response, errorCode)
}
}
function findTopDir(route) {
const reg = /^\/.+?(?=\/.+)/;
const match = route.match(reg);
return match ? match[0] : match
}
function createFilePath(urlPath) {
const parsed_url = new URL(urlPath, process.env.URL);
let filePath;
if (parsed_url.pathname == "/") {
filePath = path.join(__dirname, "frontend/html/home.html")
} else if (!path.extname(parsed_url.pathname) ){
// browser paths
const dir = path.basename(path.dirname(parsed_url.pathname));
// check for listing or article page and serve their html file
if (dir == "listing" || dir == "article") {
filePath = path.join(__dirname, `frontend/html/${dir}.html`);
} else {
filePath = path.join(__dirname, `frontend/html${parsed_url.pathname}.html`)
}
} else {
// etc files e.g favicon
filePath = path.join(__dirname, parsed_url.pathname);
}
return filePath
}
async function createStaticFilePath(urlPath) {
const parsed_url = new URL(urlPath, process.env.URL);
const pathname = parsed_url.pathname;
const dir = path.basename(path.dirname(parsed_url.pathname));
let filePath;
if (pathname == '/') {
filePath = path.join(__dirname, "static/home.html");
} else if (pathname == '/rentals' || pathname == '/sales') {
if (parsed_url.searchParams.toString()) {
// search params means someone is filtering so check if file is rendered
filePath = await handleFilterPage(parsed_url);
} else {
filePath = path.join(__dirname, `static${pathname}.html`);
}
} else if (pathname == "/articles") {
// it's an article list
let page = parsed_url.searchParams.get("page");
if (!page) page = 1;
filePath = path.join(__dirname, `static/article-lists/${page}.html`);
} else if (dir == 'listing' || dir == "article") {
const id = parsed_url.searchParams.get('id');
filePath = path.join(__dirname, `static/${dir}s/${id}.html`)
} else if (!path.extname(pathname)) {
// other browser paths
filePath = path.join(__dirname, `frontend/html/${pathname}.html`);
} else {
// etc files e.g favicon.ico, /frontend/js/home.js
filePath = path.join(__dirname, parsed_url.pathname);
}
return filePath
}
async function fileExists(filePath) {
let fileExists = null;
await fsPromises.open(filePath)
.then(fileHandle => {
if (fileHandle) fileExists = true;
fileHandle.close();
})
.catch(error => {
if (error.code == 'ENOENT') {
fileExists = false;
}
});
return fileExists
}
async function handleFilterPage(parsed_url) {
const pathname = parsed_url.pathname;
const fileName = parsed_url.search.replace('?', '');
const parentDir = pathname == '/rentals' ? "rentals-filters" : "sales-filters";
filePath = path.join(__dirname, 'static', parentDir, `${fileName}.html`);
const fileExistsResult = await fileExists(filePath);
if (!fileExistsResult) {
const { content } = await renderPage(parsed_url.href);
await fsPromises.writeFile(filePath, content);
}
return filePath
}
exports.readFileAndRespond = readFileAndRespond;
exports.createFilePath = createFilePath;
exports.createStaticFilePath = createStaticFilePath;
exports.handleError = handleError;
exports.findTopDir = findTopDir;