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.
54 lines
2.2 KiB
54 lines
2.2 KiB
//načtení knihoven
|
|
const http = require("http")
|
|
const fs = require("fs")
|
|
const path = require("path")
|
|
|
|
const server = http.createServer((req,res) => {
|
|
//nalzeni soubor za pomocí "zjednodušeného linku"
|
|
const filePath = req.url === "/" ? "pages/index.html":`pages${req.url}.html`//AltGr+ý
|
|
//načti soubor z odkazu
|
|
fs.readFile(path.join(__dirname,filePath),"utf-8",(err,data)=>{
|
|
//nejdříve zjistíme jetli chceme náš soubor nebo přesměrování
|
|
//přesměrování je dřív z důvodu ušetření načítaní dat z HTML
|
|
// if(req.url == "/presmerovani") {
|
|
// res.writeHead(302,{"Location":"https://<odkaz>"})
|
|
// res.end()
|
|
// }
|
|
if(req.url == "/git") {
|
|
res.writeHead(302,{"Location":"https://git.asgard.odbornaskola.cz"})
|
|
res.end()
|
|
} else if(req.url == "/youtube") {
|
|
res.writeHead(302,{"Location":"https://www.youtube.com/watch?v=iik25wqIuFo"})
|
|
res.end()
|
|
} else if (err) {
|
|
//ERROR HANDLING
|
|
res.writeHead(404, {"content-type":"text/html"})
|
|
res.end("<h1> 4☺4 - Page NOT Found </h1>")
|
|
} else {
|
|
//KDYŽ UŽ VÍME ŽE CHCEME SOUBOR
|
|
//aneb zapneme část kodu na zpracování
|
|
//jakéhokoliv html v složce pages
|
|
|
|
//"čistné čtení"
|
|
// res.writeHead(200,{"content-type":"text/html; charset=utf-8"})
|
|
// res.end(data)
|
|
|
|
fs.readFile(path.join(__dirname,"menu.html"),"utf-8",(err,menuContent)=>{
|
|
if(err){
|
|
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-component"></div>`,
|
|
`<div id="menu-component">${menuContent}</div>`);
|
|
//odešli upravené data společně s menu
|
|
res.writeHead(200,{"content-type":"text/html"});
|
|
res.end(data);
|
|
}
|
|
})
|
|
}
|
|
})
|
|
})
|
|
|
|
const PORT = 3000
|
|
server.listen(PORT,()=>console.log(`http://localhost:${PORT} Ctrl+C=STOP`))
|