basic node webserver

This commit is contained in:
Mike 2024-12-24 09:59:22 -05:00
parent b66089f97e
commit e3737ba712
4 changed files with 69 additions and 0 deletions

View file

@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>About</title>
<link href="css/style.css" rel="stylesheet" />
</head>
<body>
<h1>Hello There</h1>
<p>This is about as much about me as it is about you</p>
</body>
</html>

View file

@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Contact</title>
<link href="css/style.css" rel="stylesheet" />
</head>
<body>
<p>
You really shouldn't reach out to me, but if you do I'll be glad you did
</p>
</body>
</html>

View file

@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title></title>
<link href="css/style.css" rel="stylesheet" />
</head>
<body>
<h1>Welcome Home</h1>
</body>
</html>

View file

@ -0,0 +1,30 @@
const { createServer } = require("node:http");
const fs = require("fs");
const hostname = "127.0.0.1";
const port = 8080;
const pageURL = new URL("http://localhost:8080/");
const server = createServer((req, res) => {
let filename = "./src/content/index.html";
if (req.url === "/contact-me") {
filename = "./src/content/contact.html";
} else if (req.url !== "/") {
filename = "./src/content" + req.url + ".html";
}
fs.readFile(filename, (err, data) => {
if (err) {
res.writeHead(404, { "Content-Type": "text/html" });
return res.end(`<h1>404 ${filename} NOT FOUND</h1><p>${req.url}</p>`);
}
res.writeHead(200, { "Content-Type": "text/html" });
res.write(data);
return res.end();
});
});
server.listen(port, hostname, () => {
console.log(`Server running: http://${hostname}:${port}`);
});