-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalserver.js
89 lines (77 loc) · 3.53 KB
/
localserver.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
//sobre-escriu les routes per les dinàmiques
const express = require('express');
const port = 3000;
const app = express();
const path = require('path')
const fs = require('fs')
app.use('/assets', express.static('./build/'));
app.use('/assets2', express.static('../iedib-atto-snippets-misc/styles/'));
app.use('/bingo', express.static('./games/bingo/client/'));
app.get('/guiaus/:page', (req, res) => {
const p = req.params;
p.page = decodeURI(p.page);
const file = path.join("./guiaus/html/", p.page);
if(file.endsWith(".html")) {
// Serve the modified version of the file
fs.readFile(file, "utf8", function(err, data){
if(err) {
res.send(`Cannot find ${file}`);
return;
}
let parts = data.split("</head>");
const injected = `
<script type="text/x-mathjax-config">
MathJax.Hub.Config({TeX: {extensions: ["cancel.js"]},CommonHTML: { linebreaks: { automatic: true } },"HTML-CSS": { linebreaks: { automatic: true } },SVG: { linebreaks: { automatic: true } }});
</script>
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS_HTML"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css"
integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="/assets2/snippets-m384-josep.css">
<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css"
integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous" />
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.slim.js"></script>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx"
crossorigin="anonymous"></script>
<style>
body {
max-width:700px;
margin:auto;
}
</style>
`;
let newContents = parts[0] + injected + parts[1];
parts = newContents.split("<body>");
topIndex = "<ul>";
fs.readdirSync("./guiaus/html").forEach(e=>{
if(e.endsWith(".html")) {
topIndex += `<li><a href="http://localhost:${port}/guiaus/${encodeURI(e)}">${e}</a></li>`
}
});
topIndex+="</ul><hr>"
newContents = `${parts[0]}
<body>
${topIndex}
${parts[1]}
`
newContents = newContents.replace("</body>",
`
<script src="http://localhost:3000/assets/all.min.js"></script>
</body>`)
// Disable video snip on preview server
res.setHeader("content-type", "text/html");
res.send(newContents);
});
} else {
res.send(`${file} not html page`);
}
});
app.use('/', express.static(__dirname + '/guiaus/html'));
app.listen(port, () => {
console.log(`Servidor iniciado en el puerto ${port}`)
console.log(`http://localhost:${port}/guiaus/3%20Categories.html`)
});