4 changed files with 68 additions and 9 deletions
@ -1,14 +1,12 @@ |
|||||
const http = require("http"); |
|
||||
|
|
||||
const port = 3000; |
|
||||
|
const http = require("http") |
||||
|
const port = 3000 |
||||
|
|
||||
const server = http.createServer((req,res)=>{ |
const server = http.createServer((req,res)=>{ |
||||
res.statusCode = 200; |
res.statusCode = 200; |
||||
res.setHeader("Content-Type", "text/plain: charset-utf-8"); |
|
||||
|
|
||||
res.end("Ahoj z meho prvního Node.JS serveru!\n"); |
|
||||
|
res.setHeader("Content-Type","text/plain; charset=utf-8") |
||||
|
res.end("Ahoj z meho prvního NodeJS serveru!\n") |
||||
}) |
}) |
||||
|
|
||||
server.listen(port,"127.0.0.1",()=>{ |
server.listen(port,"127.0.0.1",()=>{ |
||||
console.log(`Server beží na adrese http://localhost:${port}`) //AltGR+ý=`
|
|
||||
|
console.log(`Server běží http://localhost:${port}`)//AltGr+ý=`
|
||||
}) |
}) |
||||
@ -0,0 +1,16 @@ |
|||||
|
<!DOCTYPE html> |
||||
|
<html lang="en"> |
||||
|
<head> |
||||
|
<meta charset="UTF-8"> |
||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
||||
|
<title>NODEJS</title> |
||||
|
</head> |
||||
|
<body> |
||||
|
<h1>NodeJS Webserver</h1> |
||||
|
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Aliquid aut voluptas totam enim illo ex pariatur veniam saepe, ullam officiis dicta quod similique rem est, error nihil corporis facere consequuntur?</p> |
||||
|
<p>Blanditiis velit unde nulla ullam, vitae soluta rem earum voluptas expedita provident reprehenderit maiores voluptatum dolor nostrum neque laudantium doloribus officiis consectetur ipsa dolores dignissimos explicabo nihil cum ab. Itaque!</p> |
||||
|
<p>Saepe dolores fugiat dolorum rem eos corporis, impedit optio a ipsa officiis molestias praesentium quisquam laboriosam esse libero eaque, ipsum iure. Ipsam neque, illum magnam praesentium reiciendis amet exercitationem distinctio.</p> |
||||
|
<p>Quas repellendus, temporibus reprehenderit quis amet reiciendis quae, voluptatem impedit iste et illo? Laboriosam reprehenderit incidunt, autem inventore, labore velit odio cumque vel suscipit officia blanditiis? Odio optio itaque accusamus.</p> |
||||
|
<p>Enim perferendis fuga eveniet harum temporibus aliquid eos, quaerat itaque possimus. Repudiandae, ab. Tempora, ab. Labore illum dolorum quo corporis obcaecati laudantium in autem fugit, ratione enim fugiat temporibus molestias?</p> |
||||
|
</body> |
||||
|
</html> |
||||
@ -0,0 +1,18 @@ |
|||||
|
{ |
||||
|
"name": "package_01_test", |
||||
|
"version": "1.0.0", |
||||
|
"description": "Malý program na testování NPM balíčků", |
||||
|
"keywords": [ |
||||
|
"test", |
||||
|
"wtl" |
||||
|
], |
||||
|
"license": "ISC", |
||||
|
"author": "skrabanek", |
||||
|
"type": "commonjs", |
||||
|
"main": "server.js", |
||||
|
"scripts": { |
||||
|
"test": "echo \"Error: no test specified\" && exit 1", |
||||
|
"start" : "node server.js 3000", |
||||
|
"startlinux" : "node server.js 8080" |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
//import moduly
|
||||
|
const http = require("http") |
||||
|
const fs = require("fs") |
||||
|
const path = require("path") |
||||
|
|
||||
|
const server = http.createServer((req,res)=>{ |
||||
|
console.log("REQ:",req.url) |
||||
|
const filePath = req.url === "/" ? "index.html":req.url.slice(1) //nalzeneme soubor
|
||||
|
const fullPath = path.join(__dirname,filePath) //načteme CELOU cestu
|
||||
|
console.log("otevírám:", fullPath) |
||||
|
|
||||
|
fs.readFile(fullPath,(err,content)=>{ |
||||
|
if(err){ |
||||
|
res.writeHead(404,{"content-type":"text/html; charset=utf-8"}) |
||||
|
res.end("<h1>ERROR</h1><h2>Soubor není </h2>") |
||||
|
} else { |
||||
|
res.writeHead(200,{"content-type":"text/html"}) |
||||
|
res.end(content) |
||||
|
} |
||||
|
}) |
||||
|
}) |
||||
|
|
||||
|
const PORT = process.argv[2]; |
||||
|
//const PORT = 3000;
|
||||
|
server.listen(PORT,()=>{ |
||||
|
console.log(`Server bezi http://localhost:${PORT}`) |
||||
|
}) |
||||
Loading…
Reference in new issue