9 changed files with 156 additions and 0 deletions
@ -0,0 +1,45 @@ |
|||
// načtení knihoven
|
|||
const http = require("http"); |
|||
const fs = require("fs"); |
|||
const path = require("path"); |
|||
|
|||
//vytvoření chování serveru
|
|||
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),"utf8", (err,data) => { |
|||
if (req.url == "/youtube") { |
|||
res.writeHead(302, {"Location":"https://www.youtube.com"}); |
|||
res.end(); |
|||
} |
|||
if (err) { |
|||
//když se nenalezne soubor
|
|||
res.writeHead(404, {"Content-Type" : "text/html"}); |
|||
res.end("<h1> 404 </h1"); |
|||
} else { |
|||
//když soubor se nalezne
|
|||
//přečti menu pro stránku a vlož do data
|
|||
fs.readFile(path.join(__dirname,"menu.html"),"utf8", (err,menuContent) => { |
|||
if (err) { |
|||
//když se nenalezne ani menu
|
|||
res.writeHead(500, {"Content-Type" : "text/html"}); |
|||
res.end("<h1> 500 - Internal Server Error </h1"); |
|||
} else { |
|||
//vlož menu do data stránky
|
|||
data = data.replace(`<div id="menu-con"></div>`,`<div id="menu-con">${menuContent}</div>`) |
|||
|
|||
res.writeHead(200, {"Content-Type" : "text/html"}); |
|||
res.end(data); |
|||
} |
|||
}) |
|||
} |
|||
}) |
|||
}) |
|||
|
|||
const PORT = 3000; |
|||
|
|||
//spuštění serveru
|
|||
server.listen(PORT, () => { |
|||
console.log(`Server běží na http://localhost:${PORT}`); |
|||
}) |
@ -0,0 +1,5 @@ |
|||
<ul> |
|||
<li><a href="/">HOME</a></li> |
|||
<li><a href="about">About</a></li> |
|||
<li><a href="youtube">youtube</a></li> |
|||
</ul> |
@ -0,0 +1,11 @@ |
|||
{ |
|||
"name": "webserver", |
|||
"version": "1.1.0", |
|||
"description": "maly web server", |
|||
"main": "index.js", |
|||
"scripts": { |
|||
"test": "echo \"Error: no test specified\" && exit 1" |
|||
}, |
|||
"author": "KM", |
|||
"license": "ISC" |
|||
} |
@ -0,0 +1,14 @@ |
|||
<!DOCTYPE html> |
|||
<html lang="en"> |
|||
<head> |
|||
<meta charset="UTF-8"> |
|||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> |
|||
<title>Document</title> |
|||
</head> |
|||
<body> |
|||
<h1>o nás</h1> |
|||
<br> |
|||
<div id="menu-con"></div> |
|||
</body> |
|||
</html> |
@ -0,0 +1,14 @@ |
|||
<!DOCTYPE html> |
|||
<html lang="en"> |
|||
<head> |
|||
<meta charset="UTF-8"> |
|||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> |
|||
<title>Document</title> |
|||
</head> |
|||
<body> |
|||
<h1>Vítejte na WWW stránkách</h1> |
|||
<br> |
|||
<div id="menu-con"></div> |
|||
</body> |
|||
</html> |
@ -0,0 +1,14 @@ |
|||
<!DOCTYPE html> |
|||
<html lang="en"> |
|||
<head> |
|||
<meta charset="UTF-8"> |
|||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> |
|||
<title>Document</title> |
|||
</head> |
|||
<body> |
|||
<h1>o nás</h1> |
|||
<br> |
|||
<a href="/">Home</a> |
|||
</body> |
|||
</html> |
@ -0,0 +1,14 @@ |
|||
<!DOCTYPE html> |
|||
<html lang="en"> |
|||
<head> |
|||
<meta charset="UTF-8"> |
|||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> |
|||
<title>Document</title> |
|||
</head> |
|||
<body> |
|||
<h1>Vítejte na WWW stránkách</h1> |
|||
<br> |
|||
<a href="about.html">About</a> |
|||
</body> |
|||
</html> |
@ -0,0 +1,28 @@ |
|||
// načtení knihoven
|
|||
const http = require("http"); |
|||
const fs = require("fs"); |
|||
const path = require("path"); |
|||
|
|||
//vytvoření chování serveru
|
|||
const server = http.createServer((req,res) => { |
|||
const filePath = req.url === "/" ? "index.html" : req.url.slice(1); |
|||
|
|||
const fullPath = path.join(__dirname,filePath) |
|||
|
|||
fs.readFile(fullPath, (err, content) => { |
|||
if(err) { |
|||
res.writeHead(404, {"Content-Type":"text/html"}) |
|||
res.end("<h1>404 not Found</h1>"); |
|||
} else { |
|||
res.writeHead(200, {"Content-Type":"text/html"}) |
|||
res.end(content); |
|||
} |
|||
}) |
|||
}) |
|||
|
|||
const PORT = 3000; |
|||
|
|||
//spuštění serveru
|
|||
server.listen(PORT, () => { |
|||
console.log(`Server běží na http://localhost:${PORT}`); |
|||
}) |
@ -0,0 +1,11 @@ |
|||
{ |
|||
"name": "webserver", |
|||
"version": "1.0.0", |
|||
"description": "maly web server", |
|||
"main": "index.js", |
|||
"scripts": { |
|||
"test": "echo \"Error: no test specified\" && exit 1" |
|||
}, |
|||
"author": "KM", |
|||
"license": "ISC" |
|||
} |
Loading…
Reference in new issue