//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) => {
if (req.url == "/youtube") {
//HTTP status code 302
res.writeHead(302, {"Location":"https://www.youtube.com"});
res.end();
}
else if (err){
//když soubour není
res.writeHead(404, {"Content-type":"text/html"})
res.end("
404
")
} else {
//když soubor je
// res.writeHead(200, {"Content-type" : "text/html"})
// res.end(data);
//přečti menu pro stranku a vlož do 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(" 500 - Internal Server Error
")
} else {
//vlož menu do data stránky
data = data.replace(``,
``);
//vlož data
res.writeHead(200, {"Content-type" : "text/html"})
res.end(data);
}
})
}
})
})
const PORT = 3000;
// na české klávesnici
// ` ->Pravý ALT + 7 (nad písmeny)
// $ -> pravý ALT + ů
server.listen(PORT, () => console.log(`http://localhost:${PORT} CTRL+C=STOP`))