-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
35 lines (29 loc) · 795 Bytes
/
server.js
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
const http = require("http");
const url = require("url");
// Route Handler
const routes = {
"/": (req, res) => {
res.writeHead(200, { "content-type": "text-plain" });
res.end("Welcome to Homepage");
},
"/about": (req, res) => {
res.writeHead(200, { "content-type": "text/plain" });
res.end("This is About Page");
},
"./notfound": (req, res) => {
res.writeHead(404, { "content-type": "text/plain" });
res.end("Page not Found");
},
};
const server = http.createServer((req, res) => {
const { pathname } = url.parse(req.url);
if (routes[pathname]) {
routes[pathname](req, res);
} else {
routes["./notfound"](req, res);
}
});
const port = 3000;
server.listen(port, () => {
console.log(`server is running on http://localhost:${port}`);
});