This repository has been archived by the owner on Jul 17, 2022. It is now read-only.
forked from HackYourFuture/Node.js
Nelson homework week2 #16
Open
nelnaj
wants to merge
10
commits into
HackYourFutureBelgium:class-5
Choose a base branch
from
nelnaj:nelson-homework-week2
base: class-5
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5fa75d3
add homework week 2
nelnaj 89dfff1
Refacto homework week 1
nelnaj 0b923c3
Add Json package to the project
nelnaj c5aa734
Add management Aliases
nelnaj 5c6e311
Add enable adding activities without double quota
nelnaj 2c3a5bb
Add validation to process.stdin.read() not null allowed
nelnaj 3e67c91
Add modification to homework week 1, deleting sendResponse
nelnaj 23af778
Add response when user digit a wrong command
nelnaj 4324426
Add fixes to the code
nelnaj 36d8909
Add the last fixes
nelnaj File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,31 @@ | ||
'use strict'; | ||
|
||
const urlEvaluation = require('./urlEvaluation'); | ||
const getStore = require('./responses/getStore'); | ||
const sendIndexPage = require('./responses/sendIndexPage'); | ||
const sendResponse = require('./responses/sendResponse'); | ||
const sendStyles = require('./responses/sendStyles'); | ||
|
||
function handleRequest(request, response, state) { | ||
let operation = request.url.split('').filter(x => x !== '/').join(''); | ||
|
||
if (urlEvaluation(request).urlEnable) { | ||
if (urlEvaluation(request).urlRoot) { | ||
sendIndexPage(response); | ||
} | ||
else if (urlEvaluation(request).urlStyles) { | ||
sendStyles(response); | ||
} | ||
else { | ||
state = getStore(response, state)[operation](); | ||
} | ||
} | ||
else { | ||
sendResponse.sendError(response); | ||
} | ||
|
||
response.end(); | ||
return state; | ||
} | ||
|
||
module.exports = handleRequest; |
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 |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
const { | ||
createServer | ||
} = require('./server'); | ||
|
||
const PORT = 3000; | ||
|
||
createServer().listen(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
const sendResponse = require('./sendResponse'); | ||
const DEFAULT_STATE = 10; | ||
|
||
function getStore(response, state) { | ||
return { | ||
add: () => { return setAdd(response, state); }, | ||
subtract: () => { return setSubtract(response, state); }, | ||
reset: () => { return setReset(response, state); }, | ||
state: () => { return getState(response, state); } | ||
}; | ||
} | ||
|
||
function setAdd(response, state) { | ||
state += 1; | ||
sendResponse.sendJson(response, state); | ||
return state; | ||
} | ||
|
||
function setSubtract(response, state) { | ||
state--; | ||
sendResponse.sendJson(response, state); | ||
return state; | ||
} | ||
|
||
function setReset(response, state) { | ||
state = DEFAULT_STATE; | ||
sendResponse.sendJson(response, state); | ||
return state; | ||
} | ||
|
||
function getState(response, state) { | ||
sendResponse.sendJson(response, state); | ||
return state; | ||
} | ||
|
||
module.exports = getStore; |
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,27 @@ | ||
'use strict'; | ||
|
||
function sendIndexPage(response) { | ||
response.setHeader('Content-Type', 'text/html'); | ||
response.write(` | ||
<!html> | ||
<html> | ||
<head> | ||
<title> HYF - Node.js - Week1</title> | ||
<link href="styles.css" type="text/css" rel="stylesheet"/> | ||
</head> | ||
<body> | ||
<h1> Homework 1 , Node js! </h1> | ||
This aplication return a JSON object with a value of status <br> | ||
You can use urls like: | ||
<ul> | ||
<li> /state </li> | ||
<li> /add </li> | ||
<li> /substract </li> | ||
<li> /reset </li> | ||
</ul> | ||
</body> | ||
</html> | ||
`); | ||
} | ||
|
||
module.exports = sendIndexPage; |
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,25 @@ | ||
'use strict'; | ||
|
||
const error = `{ | ||
error: "Not found" | ||
}`; | ||
|
||
function sendError(response) { | ||
response.setHeader('Content-Type', 'application/json'); | ||
response.statusCode = 404; | ||
response.write(JSON.stringify(error)); | ||
} | ||
|
||
function sendJson(response, state) { | ||
const resultToUser = { | ||
'state': state | ||
}; | ||
|
||
response.setHeader('Content-Type', 'application/json'); | ||
response.write(JSON.stringify(resultToUser)); | ||
} | ||
|
||
module.exports = { | ||
sendError, | ||
sendJson | ||
}; |
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,14 @@ | ||
'use strict'; | ||
|
||
function sendStyles(response) { | ||
response.setHeader('Content-Type', 'text/css'); | ||
response.write(` | ||
body { | ||
background: #333; | ||
color: #DDD; | ||
font-size: 1.5rem; | ||
} | ||
`); | ||
} | ||
|
||
module.exports = sendStyles; |
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,21 @@ | ||
'use strict'; | ||
|
||
function urlEvaluation(request) { | ||
const url = request.url; | ||
const supportedUrls = [ | ||
'/', | ||
'/state', | ||
'/add', | ||
'/subtract', | ||
'/reset', | ||
'/styles.css' | ||
]; | ||
|
||
return { | ||
urlRoot: url === '/', | ||
urlStyles: url === '/styles.css', | ||
urlEnable: supportedUrls.includes(url) | ||
}; | ||
} | ||
|
||
module.exports = urlEvaluation; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code from the
sendResponse
module is used directly ingetStore
to send a JSON response. As a result if you wanted to use thegetStore
module to access the state from the command line (instead of via http) you'd have to rewrite that module.This beats the purpose of splitting code in modules: the idea is that the
getStore
module is only concerned with the state and thesendResponse
only with sending a response. If you need to both access state and send a response a third module can require these two modules and combine their functionalities: in your solution that would be thehandleRequest
module.This way you leave the possibility open that if tomorrow you wanted to write a CLI you could reuse
getStore
without altering any code there.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Buccaneer Thanks for your suggestion. I modified my code and I already understand the idea.. :-) thanks a lot.