Skip to content
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

Enes Node.Js Week 1 HW #349

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion week1/homework/src/server.js
@@ -1,6 +1,7 @@
'use strict';

const http = require('http');
const urlExtension = require('./urlExtension');

/* `createServer` MUST return an instance of `http.Server` otherwise the tests
* will fail.
Expand All @@ -9,7 +10,8 @@ function createServer(port) {
let state = 10;

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

return server;
Expand Down
30 changes: 30 additions & 0 deletions week1/homework/src/urlExtension.js
@@ -0,0 +1,30 @@
/* eslint-disable */
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't disable Eslint, instead try to fix the lint errors/warnings if exist.

module.exports = {
myStateFunction: function(request, response, state) {
function writeHeadAndStringify(code, value) {
response.writeHead(code, { 'Content-Type': 'application/json' });
typeof value === 'number'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't be easier if you test the code instead of the type of value?

? response.write(JSON.stringify({ state: value }))
: response.write(JSON.stringify({ error: value }));
}
switch (request.url) {
case '/state':
writeHeadAndStringify(200, state);
break;
case '/add':
writeHeadAndStringify(200, ++state);
break;
case '/subtract':
writeHeadAndStringify(200, --state);
break;
case '/reset':
state = 10;
writeHeadAndStringify(200, state);
break;
default:
const error = 'Not found';
writeHeadAndStringify(404, error);
}
return state;
},
};