3 changed files with 42 additions and 6 deletions
@ -0,0 +1,13 @@ |
|||||
|
<!DOCTYPE html> |
||||
|
<html lang="en"> |
||||
|
<head> |
||||
|
<meta charset="UTF-8"> |
||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
||||
|
<title>Document</title> |
||||
|
</head> |
||||
|
<body> |
||||
|
<h1>About page</h1> |
||||
|
<p><a href="/">HOME</a></p> |
||||
|
<p><a href="/blal">Nefukční odkaz</a></p> |
||||
|
</body> |
||||
|
</html> |
@ -0,0 +1,12 @@ |
|||||
|
<!DOCTYPE html> |
||||
|
<html lang="en"> |
||||
|
<head> |
||||
|
<meta charset="UTF-8"> |
||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
||||
|
<title>Document</title> |
||||
|
</head> |
||||
|
<body> |
||||
|
<h1>Hello from NodeJS</h1> |
||||
|
<p><a href="/about.html">about</a></p> |
||||
|
</body> |
||||
|
</html> |
@ -1,14 +1,25 @@ |
|||||
const http = require("http"); |
const http = require("http"); |
||||
|
const fs =require("fs"); |
||||
|
const path = require("path"); |
||||
|
|
||||
const server = http.createServer((req,res) =>{ |
const server = http.createServer((req,res) =>{ |
||||
res.statusCode = 200; |
|
||||
res.setHeader("Content-type", "text/html"); |
|
||||
res.write("Gratulace k nodeJS serveru <br>"); |
|
||||
res.end("Hello world\n"); |
|
||||
}); |
|
||||
|
const filePath = req.url === "/" ? "index.html" : req.url.slice(1); |
||||
|
const fullPath = path.join(__dirname,filePath) |
||||
|
console.log("Open: "+fullPath) |
||||
|
|
||||
|
fs.readFile(fullPath,(err,content) => { |
||||
|
if(err){ |
||||
|
res.writeHead(404, {"content-type":"text/html"}); |
||||
|
//vlastní 404 error zpráva
|
||||
|
res.end("<h1> Jejda, je to rozbite <br> aneb <br> 404 </h1><a href=\"/\">HOME</a>") // (\") takzvaný eskejpování pro ignorování uvozovek v textu ohraničený stejnými uvozovkami
|
||||
|
} else { |
||||
|
res.writeHead(200, {"Content-type":"text/html"}); |
||||
|
res.end(content); |
||||
|
} |
||||
|
}) |
||||
|
}); |
||||
const PORT = 3000; |
const PORT = 3000; |
||||
server.listen(PORT, () => { |
server.listen(PORT, () => { |
||||
console.log(`Server running at http://localhost:${PORT}/ \n Ctrl+C pro vypnuti`}) |
|
||||
|
console.log(`Server running at http://localhost:${PORT}/ \n Ctrl+C pro vypnuti`) |
||||
}); |
}); |
||||
//node index.js
|
//node index.js
|
Loading…
Reference in new issue