Skip to content

wiht handling error page #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: start
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -56,7 +56,7 @@ You'll be creating a CLI program that saves and retrieves contacts from and addr
* commands
* start the server - `node exercises/api/server.js`

You'll be refacoring and fixing a simple static asset server. The server uses blocking sync code and crashes whenever a request comes in that is asking for an asset that is not found. Neither of those are desirable. You'll also have to teach the server how to server the assets it has in the `assets` folder.
You'll be refactoring and fixing a simple static asset server. The server uses blocking sync code and crashes whenever a request comes in that is asking for an asset that is not found. Neither of those are desirable. You'll also have to teach the server how to server the assets it has in the `assets` folder.

- [ ] install all remote modules (hint: use npm)
- [ ] check the README on how to run your server
11 changes: 11 additions & 0 deletions exercises/api/assets/error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1 style="color: green;">404 Error</h1>
</body>
</html>
51 changes: 34 additions & 17 deletions exercises/api/server.js
Original file line number Diff line number Diff line change
@@ -7,9 +7,16 @@ const path = require('path')
* this function is blocking, fix that
* @param {String} name full file name of asset in asset folder
*/
const findAsset = (name) => {
const assetPath = path.join(__dirname, 'assets', name)
return fs.readFileSync(assetPath, {encoding: 'utf-8'}).toString()
const findAsset = async(name) => {
const assetPath = path.join(__dirname, 'assets', name);
return new Promise((resolve,reject)=>{
fs.readFile(assetPath,{encoding:'utf-8'},(err,res)=>{
if(err)
reject(err);
else
resolve(res);
});
});
}

const hostname = '127.0.0.1'
@@ -18,21 +25,31 @@ const port = 3000
// log incoming request coming into the server. Helpful for debugging and tracking
const logRequest = (method, route, status) => console.log(method, route, status)

const server = http.createServer((req, res) => {
const method = req.method
const route = url.parse(req.url).pathname
// this is sloppy, especially with more assets, create a "router"
if (route === '/') {
res.writeHead(200, {'Content-Type': 'text/html'})
res.write(findAsset('index.html'))
logRequest(method, route, 200)
res.end()
} else {
// missing asset should not cause server crash
throw new Error('route not found')
res.end()
const routs = {
'/ GET':{
asset:'index.html',
mim:'text/html'
},
'/style.css GET':{
asset:'style.css',
mim:'text/css'
}
}
const server = http.createServer(async(req, res) => {
const method = req.method;
const route = url.parse(req.url).pathname;
const findRout = routs[`${route} ${method}`];
if (!findRout) {
res.writeHead(404);
res.write(await findAsset('error.html'))
res.end();
// console.log('ther is no rout with this url.');
} else{
res.writeHead(200, {'Content-Type': findRout.mim});
res.write(await findAsset(findRout.asset));
logRequest(method, route, 200);
res.end();
}
// most important part, send down the asset
})

server.listen(port, hostname, () => {
2,011 changes: 1,028 additions & 983 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -13,7 +13,9 @@
"dependencies": {
"body-parser": "^1.18.3",
"express": "^4.16.3",
"http": "0.0.1-security",
"mime": "^2.3.1",
"morgan": "^1.9.1"
"morgan": "^1.9.1",
"url": "^0.11.0"
}
}