-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
118 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"scripts": { | ||
"start": "node src/index.js", | ||
"dev": "nodemon src/index.js" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^14.0.5", | ||
"@types/nodemon": "^1.19.0", | ||
"nodemon": "^2.0.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!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="./style/style.css" /> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
<h1>404 Not Found</h1> | ||
<br /> | ||
<a href="/">Home</a> | ||
<a href="/about">About</a> | ||
<a href="/not-found">404</a> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!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="./style/style.css" /> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
<h1>About Page</h1> | ||
<br /> | ||
<a href="/">Home</a> | ||
<a href="/about">About</a> | ||
<a href="/not-found">404</a> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!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="./style/style.css" /> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
<h1>Home Page</h1> | ||
<br /> | ||
<a href="/">Home</a> | ||
<a href="/about">About</a> | ||
<a href="/not-found">404</a> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
html { | ||
background-color: hsla(0, 70%, 70%, 0.3) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const http = require('http'); | ||
|
||
// * ---------------- | ||
|
||
const htmlFolder = path.resolve(__dirname, '../public'); | ||
const notFoundPath = path.resolve(htmlFolder, '404.html'); | ||
|
||
const getFilePath = (url) => { | ||
const ext = path.extname(url); | ||
const missingExt = ext ? '' : '.html'; | ||
const validUrl = url === '/' ? '/index' : url; | ||
const filePath = htmlFolder + validUrl + missingExt; | ||
|
||
return filePath; | ||
}; | ||
|
||
const contentTypeMap = { | ||
'.css': 'text/css', | ||
'.html': 'text/html', | ||
}; | ||
|
||
const getContentTypeHeader = (filePath) => ({ | ||
'Content-Type': contentTypeMap[path.extname(filePath)] || '', | ||
}); | ||
|
||
http | ||
.createServer(async (req, res) => { | ||
const filePath = getFilePath(req.url); | ||
|
||
fs.readFile(filePath, (err, content) => { | ||
if (err) { | ||
if (err.code === 'ENOENT') { | ||
fs.readFile(notFoundPath, (err, content) => { | ||
res.writeHead(200, getContentTypeHeader(notFoundPath)); | ||
res.end(content, 'utf8'); | ||
}); | ||
} else { | ||
res.writeHead(500); | ||
res.end(`Server Error: ${err.code}`); | ||
} | ||
} else { | ||
res.writeHead(200, getContentTypeHeader(filePath)); | ||
res.end(content, 'utf8'); | ||
} | ||
}); | ||
}) | ||
.listen(8100, () => { | ||
console.log('http://localhost:8100/'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters