From fe13b9c152f24c22ab3babc22f342cc401075e7e Mon Sep 17 00:00:00 2001 From: mackoyokcam Date: Tue, 19 Sep 2017 15:02:10 -0700 Subject: [PATCH 1/5] initial scaffolding --- lab-mark/.eslintignore | 5 ++ lab-mark/.eslintrc.json | 26 +++++++ lab-mark/.gitignore | 145 ++++++++++++++++++++++++++++++++++++++++ lab-mark/README.md | 0 lab-mark/package.json | 23 +++++++ 5 files changed, 199 insertions(+) create mode 100644 lab-mark/.eslintignore create mode 100644 lab-mark/.eslintrc.json create mode 100644 lab-mark/.gitignore create mode 100644 lab-mark/README.md create mode 100644 lab-mark/package.json diff --git a/lab-mark/.eslintignore b/lab-mark/.eslintignore new file mode 100644 index 0000000..05b1cf3 --- /dev/null +++ b/lab-mark/.eslintignore @@ -0,0 +1,5 @@ +**/node_modules/* +**/vendor/* +**/*.min.js +**/coverage/* +**/build/* diff --git a/lab-mark/.eslintrc.json b/lab-mark/.eslintrc.json new file mode 100644 index 0000000..840d336 --- /dev/null +++ b/lab-mark/.eslintrc.json @@ -0,0 +1,26 @@ +{ + "env": { + "browser": true, + "node": true, + "commonjs": true, + "jest": true, + "es6": true + }, + "globals": { + "err": true, + "req": true, + "res": true, + "next": true + }, + "extends": "eslint:recommended", + "parserOptions": { + "sourceType": "module" + }, + "rules": { + "no-console": "off", + "indent": [ "error", 2 ], + "quotes": ["error", "single", { "allowTemplateLiterals": true }], + "comma-dangle": ["error", "always-multiline"], + "semi": [ "error", "always" ] + } +} diff --git a/lab-mark/.gitignore b/lab-mark/.gitignore new file mode 100644 index 0000000..9497282 --- /dev/null +++ b/lab-mark/.gitignore @@ -0,0 +1,145 @@ +# 401 JS +db +.env +temp +build + +# Created by https://www.gitignore.io/api/vim,osx,node,linux,windows +### Linux ### +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +### Node ### +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Typescript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env + + +### OSX ### +*.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### Vim ### +# swap +[._]*.s[a-v][a-z] +[._]*.sw[a-p] +[._]s[a-v][a-z] +[._]sw[a-p] +# session +Session.vim +# temporary +.netrwhist +# auto-generated tag files +tags + +### Windows ### +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# End of https://www.gitignore.io/api/vim,osx,node,linux,windows diff --git a/lab-mark/README.md b/lab-mark/README.md new file mode 100644 index 0000000..e69de29 diff --git a/lab-mark/package.json b/lab-mark/package.json new file mode 100644 index 0000000..eea6343 --- /dev/null +++ b/lab-mark/package.json @@ -0,0 +1,23 @@ +{ + "name": "lab-mark", + "version": "1.0.0", + "description": "", + "main": "index.js", + "directories": { + "lib": "lib" + }, + "scripts": { + "lint": "eslint .", + "test": "jest --coverage -i", + "test-watch": "jest --watch -i" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "jest": "^21.1.0" + }, + "dependencies": { + "faker": "^4.1.0" + } +} From 33c873ba1680160808aba299fdbd0807dce33a87 Mon Sep 17 00:00:00 2001 From: mackoyokcam Date: Tue, 19 Sep 2017 21:34:32 -0700 Subject: [PATCH 2/5] Mostly working --- lab-mark/__test__/server.test.js | 34 ++++++++++++ lab-mark/index.js | 11 ++++ lab-mark/lib/request-parser.js | 32 ++++++++++++ lab-mark/lib/server.js | 89 ++++++++++++++++++++++++++++++++ lab-mark/package.json | 10 +++- 5 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 lab-mark/__test__/server.test.js create mode 100644 lab-mark/index.js create mode 100644 lab-mark/lib/request-parser.js create mode 100644 lab-mark/lib/server.js diff --git a/lab-mark/__test__/server.test.js b/lab-mark/__test__/server.test.js new file mode 100644 index 0000000..06885ca --- /dev/null +++ b/lab-mark/__test__/server.test.js @@ -0,0 +1,34 @@ +'use strict'; + +const superagent = require('superagent'); + +describe('POST /api/cowsay', () => { + test('should respond with 200 response and echo the body', () => { + return superagent.post('http://localhost:3000/api/cowsay') + .send({ + text: 'Wassup', + }) + .then(res => { + expect(res.status).toEqual(200); + expect(res.body).toEqual({'content': ` ________ +< Wassup > + -------- + \\ ^__^ + \\ (oo)\\_______ + (__)\\ )\\/\\ + ||----w | + || ||`}); + }); + }); + + test('should respond with a 400', () => { + return superagent.post('http://localhost:3000/api/cosway') + .set({ 'Content-Type': 'application/json'}) + .send('{') + .then(Promise.reject) + .catch(res => { + expect(res.status).toEqual(400); + expect(res.response.text).toEqual('bad request'); + }); + }); +}); diff --git a/lab-mark/index.js b/lab-mark/index.js new file mode 100644 index 0000000..a048fbb --- /dev/null +++ b/lab-mark/index.js @@ -0,0 +1,11 @@ +'use strict'; + +// load environment +// require('dotenv').config(); +const PORT = process.env.PORT || 3000; + +const server = require('./lib/server.js'); + +// start server +server.start(PORT, () => + console.log('server up ::', PORT)); diff --git a/lab-mark/lib/request-parser.js b/lab-mark/lib/request-parser.js new file mode 100644 index 0000000..963e504 --- /dev/null +++ b/lab-mark/lib/request-parser.js @@ -0,0 +1,32 @@ +'use strict'; + +const url = require('url'); +const queryString = require('querystring'); +const cowsay = require('cowsay'); + +module.exports = (req) => { + return new Promise((resolve, reject) => { + req.url = url.parse(req.url); + req.url.query = queryString.parse(req.url.query); + console.log(req.url.query); + + if(!(req.method === 'POST' || req.method === 'PUT')) + return resolve(req); + + req.body = {}; + let content = ''; + // ONLY PARSER THE POST OR PUT REQUEST BODYS + req.on('data', (buffer) => { + content += buffer.toString(); + }); + req.on('end', () => { + try { + console.log(content); + req.body.content = cowsay.say(JSON.parse(content)); + resolve(req); + } catch (err) { + reject(err); + } + }); + }); +}; diff --git a/lab-mark/lib/server.js b/lab-mark/lib/server.js new file mode 100644 index 0000000..83aab8d --- /dev/null +++ b/lab-mark/lib/server.js @@ -0,0 +1,89 @@ +'use strict'; + +const http = require('http'); +const requestParser = require('./request-parser.js'); +const cowsay = require('cowsay'); + +const app = http.createServer((req, res) => { + + requestParser(req) + .then(req => { + // handle rotues + if(req.method === 'GET' && req.url.pathname === '/'){ + res.writeHead(200, {'Content-Type': 'text/html'}); + res.write(` + + + + cowsay + + +
+ +
+
+ Cowsay Project, pew pew! +
+ + + `); + res.end(); + return; // break out of the (req, res) => {} callback + } + + if(req.method === 'GET' && req.url.pathname === '/cowsay'){ + let cowMessage = { text: 'I wanna say something!' }; + if (req.url.query.text) + cowMessage = req.url.query; + + res.writeHead(200, {'Content-Type': 'text/html'}); + res.write(` + + + + cowsay + + +

cowsay

+
+                ${cowsay.say(cowMessage)}
+              
+ + + `); + res.end(); + return; // break out of the (req, res) => {} callback + } + + if(req.method === 'POST' && req.url.pathname === '/api/cowsay'){ + + res.writeHead(200, {'Content-Type': 'application/json'}); + res.write(JSON.stringify(req.body)); + res.end(); + return; // break out of the (req, res) => {} callback + } + + // 404 for any request to a non route + // respond to the client + res.writeHead(404, { + 'Content-Type': 'text/plain', + }); + res.write(`resource ${req.url.pathname} not found!`); + res.end(); + }) + .catch(err => { + console.log(err); + res.writeHead(400, { 'Content-Type': 'text/plain' }); + res.write('bad request'); + res.end(); + }); +}); + +module.exports = { + start: (port, callback) => app.listen(port, callback), + stop: (callback) => app.close(callback), +}; diff --git a/lab-mark/package.json b/lab-mark/package.json index eea6343..ba22a0d 100644 --- a/lab-mark/package.json +++ b/lab-mark/package.json @@ -7,6 +7,8 @@ "lib": "lib" }, "scripts": { + "start": "node index.js", + "watch": "nodemon index.js", "lint": "eslint .", "test": "jest --coverage -i", "test-watch": "jest --watch -i" @@ -15,9 +17,13 @@ "author": "", "license": "ISC", "devDependencies": { - "jest": "^21.1.0" + "dotenv": "^4.0.0", + "jest": "^21.1.0", + "nodemon": "^1.12.1" }, "dependencies": { - "faker": "^4.1.0" + "cowsay": "^1.2.1", + "faker": "^4.1.0", + "superagent": "^3.6.0" } } From f2aad67d3cf3f6eab12686eabe5aa68b5d5ab2a9 Mon Sep 17 00:00:00 2001 From: Mackoyokcam Date: Tue, 19 Sep 2017 23:03:18 -0700 Subject: [PATCH 3/5] MVP, can't return 400 for bad get request --- lab-mark/.gitignore | 2 ++ lab-mark/__test__/server.test.js | 29 +++++++++++++++++++++++++++++ lab-mark/lib/request-parser.js | 4 ++-- lab-mark/lib/server.js | 17 +++++++++++++++-- 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/lab-mark/.gitignore b/lab-mark/.gitignore index 9497282..3184cd5 100644 --- a/lab-mark/.gitignore +++ b/lab-mark/.gitignore @@ -1,4 +1,6 @@ # 401 JS +package-lock.json +../package-lock.json db .env temp diff --git a/lab-mark/__test__/server.test.js b/lab-mark/__test__/server.test.js index 06885ca..366985a 100644 --- a/lab-mark/__test__/server.test.js +++ b/lab-mark/__test__/server.test.js @@ -32,3 +32,32 @@ describe('POST /api/cowsay', () => { }); }); }); + +describe('GET /api/cowsay', () => { + test('should respond with 200 response and echo the body', () => { + return superagent.get('http://localhost:3000/api/cowsay') + .query('text=lulwat') + .then(res => { + expect(res.status).toEqual(200); + expect(res.body).toEqual({'content': ` ________ +< lulwat > + -------- + \\ ^__^ + \\ (oo)\\_______ + (__)\\ )\\/\\ + ||----w | + || ||`}); + }); + }); + + test('should respond with a 400', () => { + return superagent.get('http://localhost:3000/api/cosway') + .set({ 'Content-Type': 'application/json'}) + .query('txt=lulwat') + .then(Promise.reject) + .catch(res => { + expect(res.status).toEqual(400); + expect(res.response.text).toEqual('bad request'); + }); + }); +}); diff --git a/lab-mark/lib/request-parser.js b/lab-mark/lib/request-parser.js index 963e504..5d5287d 100644 --- a/lab-mark/lib/request-parser.js +++ b/lab-mark/lib/request-parser.js @@ -8,14 +8,14 @@ module.exports = (req) => { return new Promise((resolve, reject) => { req.url = url.parse(req.url); req.url.query = queryString.parse(req.url.query); - console.log(req.url.query); + // Resolve if GET if(!(req.method === 'POST' || req.method === 'PUT')) return resolve(req); req.body = {}; let content = ''; - // ONLY PARSER THE POST OR PUT REQUEST BODYS + req.on('data', (buffer) => { content += buffer.toString(); }); diff --git a/lab-mark/lib/server.js b/lab-mark/lib/server.js index 83aab8d..11960d8 100644 --- a/lab-mark/lib/server.js +++ b/lab-mark/lib/server.js @@ -26,7 +26,9 @@ const app = http.createServer((req, res) => {
- Cowsay Project, pew pew! + Click on the cowsay anchor tag to get the default cow and cow text.
+ To change the text, add '?text=message to the end of the url.
+ To change the cow, add &f=dragon after the message.'
@@ -36,7 +38,7 @@ const app = http.createServer((req, res) => { } if(req.method === 'GET' && req.url.pathname === '/cowsay'){ - let cowMessage = { text: 'I wanna say something!' }; + let cowMessage = { text: 'I need something good to say!' }; if (req.url.query.text) cowMessage = req.url.query; @@ -59,6 +61,17 @@ const app = http.createServer((req, res) => { return; // break out of the (req, res) => {} callback } + if (req.method === 'GET' && req.url.pathname === '/api/cowsay') { + let cowMessage = { text: 'I need something good to say!' }; + if (req.url.query.text) + cowMessage = req.url.query; + + res.writeHead(200, {'Content-Type': 'application/json'}); + res.write(JSON.stringify({content: cowsay.say(cowMessage)})); + res.end(); + return; + } + if(req.method === 'POST' && req.url.pathname === '/api/cowsay'){ res.writeHead(200, {'Content-Type': 'application/json'}); From 282a3075d8fa0a1a5fe356d71e5c8715f6b8eec7 Mon Sep 17 00:00:00 2001 From: Mackoyokcam Date: Tue, 19 Sep 2017 23:14:26 -0700 Subject: [PATCH 4/5] set up .env, works! --- lab-mark/index.js | 8 +++----- lab-mark/lib/server.js | 4 +--- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/lab-mark/index.js b/lab-mark/index.js index a048fbb..5e60742 100644 --- a/lab-mark/index.js +++ b/lab-mark/index.js @@ -1,11 +1,9 @@ 'use strict'; // load environment -// require('dotenv').config(); -const PORT = process.env.PORT || 3000; - +require('dotenv').config(); const server = require('./lib/server.js'); // start server -server.start(PORT, () => - console.log('server up ::', PORT)); +server.start(process.env.PORT, () => + console.log('server up ::', process.env.PORT)); diff --git a/lab-mark/lib/server.js b/lab-mark/lib/server.js index 11960d8..2b45343 100644 --- a/lab-mark/lib/server.js +++ b/lab-mark/lib/server.js @@ -51,9 +51,7 @@ const app = http.createServer((req, res) => {

cowsay

-
-                ${cowsay.say(cowMessage)}
-              
+
${cowsay.say(cowMessage)}
`); From 3b0cf651875258952c25c574f064a6fce8ad5ad0 Mon Sep 17 00:00:00 2001 From: Mackoyokcam Date: Tue, 19 Sep 2017 23:30:19 -0700 Subject: [PATCH 5/5] added readme --- lab-mark/README.md | 14 ++++++++++++++ lab-mark/lib/request-parser.js | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lab-mark/README.md b/lab-mark/README.md index e69de29..47c5582 100644 --- a/lab-mark/README.md +++ b/lab-mark/README.md @@ -0,0 +1,14 @@ +![CF](https://camo.githubusercontent.com/70edab54bba80edb7493cad3135e9606781cbb6b/687474703a2f2f692e696d6775722e636f6d2f377635415363382e706e67) Code-401-Javascript lab-7: HTTP-Server +=== +This is the day 7 lab with Code Fellows. The purpose of the lab is to teach students to use vanilla HTTP requests to create a server. + +# Modules +Description of exported values of each module defined in lib/ directory, along with airity and expected input/return values. +### Server.js +Houses the code for the server. It exports an object with start and stop methods which either starts or stops the server. Currently only start is being invoked in index.js. +* #### start + * Has an arity of two, which takes in a port number and callback function. Port number is preset to 3000 and the callback is a success function which logs to the server which port the server is running on. +* #### stop + * Has an arity of one, which takes in a callback function. Currently not being utilized. +### request-parser.js +Exports a function with an arity of one, that takes in a request and returns a new promise. It parses the data depending if its a GET or POST request. diff --git a/lab-mark/lib/request-parser.js b/lab-mark/lib/request-parser.js index 5d5287d..ad74b8b 100644 --- a/lab-mark/lib/request-parser.js +++ b/lab-mark/lib/request-parser.js @@ -8,7 +8,7 @@ module.exports = (req) => { return new Promise((resolve, reject) => { req.url = url.parse(req.url); req.url.query = queryString.parse(req.url.query); - + console.log(req.url.query); // Resolve if GET if(!(req.method === 'POST' || req.method === 'PUT')) return resolve(req);