-
Notifications
You must be signed in to change notification settings - Fork 0
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
6 changed files
with
119 additions
and
1 deletion.
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,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}`)); |
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
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,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> |
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,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> |
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,4 @@ | ||
body { | ||
background: #333; | ||
color: #ffff; | ||
} |
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,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> |