Skip to content

Commit

Permalink
web server
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmagical committed Jun 27, 2023
1 parent b4bde1b commit 3010616
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 1 deletion.
77 changes: 77 additions & 0 deletions node_crash_course/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const http = require('http');
const path = require('path');
const fs = require('fs');

const server = http.createServer((req, res) => {
// if(req.url === '/') {
// fs.readFile(
// path.join(__dirname, 'public', 'index.html'),
// (err, content) => {
// //if (err) throw err;
// res.writeHead(200, { 'Content-Type': 'text/html'});
// res.end(content);
// }
// );
// }

// if(req.url === '/api/users') {
// const users = [
// { name: 'manny jones', age: 19},
// {name: 'alex johnson', age: 39}
// ];
// res.writeHead(200, { 'Content-Type': 'application/json'});
// res.end(JSON.stringify(users));
// }


//build filepath
let filePath = path.join(__dirname, 'public', req.url === '/' ?
'index.html' : req.url);

//get extension of file
let extname = path.extname(filePath);

//initial content type
let contentType = 'text/html';

//check the ext and set content type
switch(extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
case '.json':
contentType = 'application/json';
break;
case '.png':
contentType = 'image/png';
break;
case '.jpg':
contentType = 'image.jpg';
break;
}

//read file
fs.readFile(filePath, (err, content) => {
if(err) {
if(err.code == 'ENOENT') {
//page not found
fs.readFile(path.join(__dirname, 'public', '404.html'), (err, content)
=> {
res.writeHead(200, { 'Content-Type': 'text/html'});
res.end(content, 'utf8');
})
} else {
//success
res.writeHead(200, { 'Content-Type': contentType});
res.end(content, 'utf8');
}
}
});
});

const PORT = process.env.PORT || 5000;

server.listen(PORT, () => console.log(`Server running on port ${PORT}`));
3 changes: 2 additions & 1 deletion node_crash_course/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "node crash course",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"start": "node index",
"dev": "nodemon index"
},
"author": "manny jones",
"license": "ISC",
Expand Down
12 changes: 12 additions & 0 deletions public/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./css/stlye.css">
<title>Not Found</title>
</head>
<body>
<h1>404 not found</h1>
</body>
</html>
12 changes: 12 additions & 0 deletions public/about.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./css/stlye.css">
<title>About</title>
</head>
<body>
<h1>About</h1>
</body>
</html>
4 changes: 4 additions & 0 deletions public/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
background: #333;
color: #ffff;
}
12 changes: 12 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./css/stlye.css">
<title>Homepage</title>
</head>
<body>
<h1> Welcome To Tha Homepage!</h1>
</body>
</html>

0 comments on commit 3010616

Please sign in to comment.