Skip to content

Commit 3b02b9a

Browse files
committed
changed structure
1 parent 5d529a3 commit 3b02b9a

File tree

7 files changed

+67
-34
lines changed

7 files changed

+67
-34
lines changed

routing.js

-34
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.

server/routing.express.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//convert the above code to express js
2+
const express = require("express");
3+
const fs = require("fs");
4+
5+
const app = express();
6+
7+
app.get("/", (req, res) => {
8+
const content = fs.readFileSync("./pages/hello.html");
9+
res.writeHead(200, { "Content-Type": "text/html" });
10+
return res.end(content);
11+
});
12+
13+
app.get("/about", (req, res) => {
14+
const content = fs.readFileSync("./pages/about.html");
15+
res.writeHead(200, { "Content-Type": "text/html" });
16+
return res.end(content);
17+
});
18+
19+
app.get("/contact", (req, res) => {
20+
const content = fs.readFileSync("./pages/contact.html");
21+
res.writeHead(200, { "Content-Type": "text/html" });
22+
return res.end(content);
23+
});
24+
25+
app.post("/", (req, res) => {
26+
fs.writeFileSync("hello.html", "Hello from Node.js");
27+
res.writeHead(200, { "Content-Type": "text/html" });
28+
return res.end("File saved");
29+
});
30+
31+
app.listen(9999, () => {
32+
console.log("Server running at port 9999");
33+
});

server/routing.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// node js server with routing
2+
const http = require("http");
3+
const fs = require("fs");
4+
5+
const PAGES = "PAGES";
6+
7+
const server = http.createServer((req, res) => {
8+
if (req.method === "GET") {
9+
if (req.url === "/") {
10+
const content = fs.readFileSync(`./${PAGES}/hello.html`);
11+
res.writeHead(200, { "Content-Type": "text/html" });
12+
return res.end(content);
13+
}
14+
if (req.url === "/about") {
15+
const content = fs.readFileSync(`./${PAGES}/about.html`);
16+
res.writeHead(200, { "Content-Type": "text/html" });
17+
return res.end(content);
18+
}
19+
if (req.url === "/contact") {
20+
const content = fs.readFileSync(`./${PAGES}/contact.html`);
21+
res.writeHead(200, { "Content-Type": "text/html" });
22+
return res.end(content);
23+
}
24+
}
25+
if (req.method === "POST") {
26+
fs.writeFileSync("hello.html", "Hello from Node.js");
27+
res.writeHead(200, { "Content-Type": "text/html" });
28+
return res.end("File saved");
29+
}
30+
});
31+
32+
server.listen(9999, () => {
33+
console.log("Server running at port 9999");
34+
});

server.js server/server.js

File renamed without changes.

0 commit comments

Comments
 (0)