Skip to content
Snippets Groups Projects
Commit 008b6a7c authored by atm2g19's avatar atm2g19
Browse files

Created html-rendere

added mobile-compatibility to html
parent d4b0ee7f
Branches
No related tags found
No related merge requests found
......@@ -2,17 +2,14 @@
<html>
<head>
#{meta}
<link rel="stylesheet" href="/css/main.css">
</head>
<body>
<div id="header">
<h1>
<a href="/" id="home">
Alex Mansfield's web server!
</a>
</h1>
</div>
#{header}
<div id="subheader">
<h1>
......@@ -49,9 +46,7 @@
</ul>
</div>
<div id="footer">
</div>
#{footer}
</body>
</html>
\ No newline at end of file
......@@ -2,6 +2,9 @@
<html>
<head>
#{meta}
<title>Battleships</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js"></script>
<script src="https://cdn.jsdelivr.net/npm/p5@1.0.0/lib/p5.min.js"></script>
......@@ -11,13 +14,7 @@
</head>
<body>
<div id="header">
<h1>
<a href="/" id="home">
Alex Mansfield's web server!
</a>
</h1>
</div>
#{header}
<div id="subheader">
<h1>
......@@ -29,9 +26,7 @@
<div id="container">
</div>
<div id="footer">
</div>
#{footer}
</body>
</html>
\ No newline at end of file
<div id="footer">
</div>
\ No newline at end of file
<div id="header">
<h1>
<a href="/" id="home">
Alex Mansfield's web server!
</a>
</h1>
</div>
\ No newline at end of file
<meta name="viewport" content="width=device-width, initial-scale=1.0">
\ No newline at end of file
<div id="subheader">
<h1>
Error
</h1>
</div>
\ No newline at end of file
......@@ -2,18 +2,15 @@
<html>
<head>
#{meta}
<title>AM Web!</title>
<link rel="stylesheet" href="/css/main.css">
</head>
<body>
<div id="header">
<h1>
<a href="/" id="home">
Alex Mansfield's web server!
</a>
</h1>
</div>
#{header}
<div id="container">
<ul>
<li>
......@@ -27,9 +24,7 @@
</li>
</ul>
</div>
<div id="footer">
</div>
#{footer}
</body>
</html>
\ No newline at end of file
......@@ -9,6 +9,31 @@ function staticServe(root: string) {
}
function sendFile(path: string, req: express.Request, res: express.Response, next: express.NextFunction) {
let type = path.substr(path.lastIndexOf('.'));
getFile(path)
.then(async file => {
let r = /#{.+}/gm;
let i;
if (type == '.html')
while (i = r.exec(file)) {
// console.log(i[0]);
try {
let include = await getFile(`public/include/${i[0].substring(2, i[0].length-1)}.html`);
file = file.substr(0, i.index) + include + file.substr(i.index + i[0].length);
} catch (e) {
file = file.substr(0, i.index) + file.substr(i.index + i[0].length);
}
}
res.type(type);
res.send(file);
res.end();
})
.catch(err => {
console.log(err);
next(404);
})
return;
fs.exists(path, (exists: boolean) => {
if (!exists) {
next(404);
......@@ -34,6 +59,16 @@ function sendFile(path: string, req: express.Request, res: express.Response, nex
})
}
async function getFile(path: string): Promise < string > {
// console.log(`Getting file: ${path}`);
let handle = await fs.promises.open(path, 'r');
// console.log("HANDLE");
// console.log(handle);
let file = await handle.readFile();
handle.close();
return file.toString();
}
export {
sendFile,
staticServe
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment