-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6ServerExample.js
More file actions
39 lines (34 loc) · 1.24 KB
/
6ServerExample.js
File metadata and controls
39 lines (34 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//first see previous codes: 5.0server.js, 5.1server.js and 5.2server.js
//here we will load file and display:
//Creating html pages for home, about and contact to display them using file system and server on web browser
//These files are inside HtmlPages folder
const http = require("http"); //used to work with servers
const fs=require("fs"); //used to work with files
const port = 5000;
const hostname = "localhost";
//reading html files in synchronous mode
//in synchronous mode because i want the files to load first then display
const home=fs.readFileSync('./HtmlPages/home.html',"utf-8");
const about=fs.readFileSync('./HtmlPages/about.html',"utf-8");
const contact=fs.readFileSync('./HtmlPages/contact.html',"utf-8");
const work=fs.readFileSync("./HtmlPages/work.html","utf-8");
const server = http.createServer((req, res) => {
if (req.url === "/" || req.url === "/home") {
return res.end(home);
}
if (req.url === "/about") {
return res.end(about);
}
if (req.url === "/contact") {
return res.end(contact);
}
if(req.url==="/work"){
return res.end(work)
}
else {
return res.end("<h1>404 Page Not Found!</h1>");
}
});
server.listen(port,hostname,()=>{
console.log(`Running server is: http://${hostname}:${port}`);
})