You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.9 KiB
50 lines
1.9 KiB
//načtení knihoven
|
|
const http = require("http");
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
const server = http.createServer((req,res) => {
|
|
//přečti URL
|
|
const filePath = req.url === "/" ? "pages/index.html" : `pages${req.url}.html`
|
|
//načti hledaný soubor
|
|
fs.readFile(path.join(__dirname,filePath),"utf-8",(err,data) => {
|
|
//odkazy s přesměrováním
|
|
if (req.url == "/youtube"){
|
|
res.writeHead(302, {"Location":"https://www.youtube.com"});
|
|
res.end();
|
|
}
|
|
else if (req.url == "/git"){
|
|
res.writeHead(302, {"Location":"https://git.asgard.odbornaskola.cz"});
|
|
res.end();
|
|
}
|
|
else if (err) {
|
|
//error handling
|
|
res.writeHead(404, {"Content-type":"text/html"});
|
|
res.end("<h1> 404 - Page not found </h1>");
|
|
} else {
|
|
//když soubor je
|
|
// res.writeHead(200, {"Content-type":"text/html"});
|
|
// res.end(data);
|
|
|
|
fs.readFile(path.join(__dirname,"menu.html"), "utf-8", (err,menuContent) =>{
|
|
if(err) {
|
|
//když server nenalezne menu.html
|
|
res.writeHead(500, {"Content-type":"text/html"});
|
|
res.end("<h1> 500 - Internal Server Error </h1>")
|
|
} else {
|
|
//vlož menu pro stránku do DATA stránky
|
|
data = data.replace(`<div id="menu-con"></div>`,
|
|
`<div id="menu-con">${menuContent}</div>`);
|
|
//odešli upravené data
|
|
res.writeHead(200, {"Content-type":"text/html"});
|
|
res.end(data);
|
|
}
|
|
})
|
|
}
|
|
})
|
|
});
|
|
// na české klávesnící
|
|
// ` -> AltGR + ý
|
|
// $ -> AltGR + ů
|
|
const PORT = 3000;
|
|
server.listen(PORT, ()=>console.log(`http://localhost:${PORT} Ctrl+c=STOP`))
|