Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
63 lines (55 loc) · 2.2 KB
/
Copy pathserver.js
File metadata and controls
63 lines (55 loc) · 2.2 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// creating a server using http module
// importing the http module
const http = require("http");
// creating a server
const server = http.createServer((req, res) => {
if (req.url === "/") {
// if the route is "/" i.e. home route then the welcome message will appear
res.writeHead(200, { "content-type": "text/html" });
res.end(`
<h1 style='color:black'> Welcome to my http server !! </h1>
`);
console.log("Welcome to my http server !!");
} else if (req.url === "/about") {
// if the route is "/about" then the about page will appear and with a corresponding message
res.writeHead(200, { "content-type": "text/html" });
res.end(`
<h1 style='color:green'>About Page</h1>
<p> Hola, Myself Harsh Dev Jha.
</br> I am currently doing B.Tech CSE Core from
</br>
K.R. Mangalam University,
Gurugram, Haryana
</p>
`);
console.log("About Page");
console.log(
"Hola, Myself Harsh Dev Jha. I am currently doing B.Tech CSE Core from K.R. Mangalam University, Gurugram, Haryana ",
);
} else if (req.url === "/contact") {
// if the route is "/contact" then the contact page will appear with the corresponding message
res.writeHead(200, { "content-type": "text/html" });
res.end(`
<h1 style='color:red'>Contact Page</h1>
<p> Mobile Number : +91-1234567890
</br> Email : abc@gmail.com
</p>
`);
console.log("Contact Page");
console.log("Mobile Number : +91-1234567890 ,Email : abc@gmail.com");
} else {
// handling the routes that doesnot exist with error message
res.writeHead(404, { "content-type": "text/html" });
res.end(`
<h1 style='color:red'>404 : Not Found Error !!</h1>
`);
console.log("404 : Not Found Error !!");
}
});
// running the server
server.listen(10000, () => {
console.log("Server is running !!");
}); // generally 5000
// to run the code run the following command :
// node server.js
// and open postman or any browser to test different routes