Skip to content
This repository has been archived by the owner on Jul 17, 2022. It is now read-only.

Nelson homework week2 #16

Open
wants to merge 10 commits into
base: class-5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions week1/homework/src/handleRequest.js
@@ -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;
1 change: 0 additions & 1 deletion week1/homework/src/index.js
Expand Up @@ -3,7 +3,6 @@
const {
createServer
} = require('./server');

const PORT = 3000;

createServer().listen(PORT, () => {
Expand Down
37 changes: 37 additions & 0 deletions week1/homework/src/responses/getStore.js
@@ -0,0 +1,37 @@
'use strict';
const sendResponse = require('./sendResponse');
Copy link
Collaborator

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 in getStore to send a JSON response. As a result if you wanted to use the getStore 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 the sendResponse 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 the handleRequest 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.

Copy link
Author

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.

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;
27 changes: 27 additions & 0 deletions week1/homework/src/responses/sendIndexPage.js
@@ -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;
25 changes: 25 additions & 0 deletions week1/homework/src/responses/sendResponse.js
@@ -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
};
14 changes: 14 additions & 0 deletions week1/homework/src/responses/sendStyles.js
@@ -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;
12 changes: 3 additions & 9 deletions week1/homework/src/server.js
@@ -1,17 +1,11 @@
'use strict';

const http = require('http');

/* `createServer` MUST return an instance of `http.Server` otherwise the tests
* will fail.
*/
function createServer(port) {
const handleRequest = require('./handleRequest');
function createServer() {
let state = 10;

const server = http.createServer((request, response) => {
// TODO: Write your homework code here
state = handleRequest(request, response, state);
});

return server;
}

Expand Down
21 changes: 21 additions & 0 deletions week1/homework/src/urlEvaluation.js
@@ -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;