From a1cd4d90c003807d2e1b1776da8b33fa5b4a1c4e Mon Sep 17 00:00:00 2001 From: Christina Thomas Date: Tue, 19 Sep 2017 23:52:06 -0700 Subject: [PATCH 01/11] server.js/adding routes --- lab-christina/.eslintignore | 0 lab-christina/.eslintrc | 0 lab-christina/.gitignore | 128 ++++++++++++++++++++++++++++++++++++ lab-christina/README.md | 0 lab-christina/index.js | 0 lab-christina/lib/server.js | 46 +++++++++++++ lab-christina/package.json | 15 +++++ 7 files changed, 189 insertions(+) create mode 100644 lab-christina/.eslintignore create mode 100644 lab-christina/.eslintrc create mode 100644 lab-christina/.gitignore create mode 100644 lab-christina/README.md create mode 100644 lab-christina/index.js create mode 100644 lab-christina/lib/server.js create mode 100644 lab-christina/package.json diff --git a/lab-christina/.eslintignore b/lab-christina/.eslintignore new file mode 100644 index 0000000..e69de29 diff --git a/lab-christina/.eslintrc b/lab-christina/.eslintrc new file mode 100644 index 0000000..e69de29 diff --git a/lab-christina/.gitignore b/lab-christina/.gitignore new file mode 100644 index 0000000..8922811 --- /dev/null +++ b/lab-christina/.gitignore @@ -0,0 +1,128 @@ + +# Created by https://www.gitignore.io/api/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 + +### 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/osx,node,linux,windows diff --git a/lab-christina/README.md b/lab-christina/README.md new file mode 100644 index 0000000..e69de29 diff --git a/lab-christina/index.js b/lab-christina/index.js new file mode 100644 index 0000000..e69de29 diff --git a/lab-christina/lib/server.js b/lab-christina/lib/server.js new file mode 100644 index 0000000..b8374b2 --- /dev/null +++ b/lab-christina/lib/server.js @@ -0,0 +1,46 @@ +const HTTP = require('http'); +const REQUESTPARSER = require('./request-parser.js'); + +const APP = HTTP.createServer((request, response) => { + console.log('request.method', request.method); + console.log('request.headers', request.headers); + +REQUESTPARSER(request) +.then(request => { + if(request.method === 'GET' && request.url.pathname === '/'){ + response.writeHead(200, {'Content-Type': 'text/html'}); + response.write(` + + Hello Universe +

Goodnight Moon

+ `); + response.end(); + return; + } + + if(request.method === 'POST' && request.url.pathname === '/echo'){ + response.writeHead(200, {'Content-Type': 'application/json'}); + response.write(JSON.stringify(request.body)); + response.end(); + return; + }; + + response.writeHead(404, { + 'Content-Type': 'text/plain'; + }); + response.write(`resource ${req.url.pathname} not found!`) + res.end(); +}); +.catch(err => { + console.log(err); + response.writeHead(400, {'Content-Type': 'text/plain'}); + response.write('bad request'); + response.end(); +}); +//register routes +}); + +module.exports = { + start: (PORT, callback) => APP.listen(PORT, callback), + stop: (callback) => APP.close(callback), +}; diff --git a/lab-christina/package.json b/lab-christina/package.json new file mode 100644 index 0000000..712acd0 --- /dev/null +++ b/lab-christina/package.json @@ -0,0 +1,15 @@ +{ + "name": "lab-christina", + "version": "1.0.0", + "description": "''\u001b[Dlab assignment - http server'", + "main": "index.js", + "scripts": { + "test": "jest -i --coverage" + }, + "repository": { + "type": "git", + "url": "\u0016" + }, + "author": "", + "license": "MIT" +} From 01fefbbae7886eeda7cf92ff9324acac9799cca4 Mon Sep 17 00:00:00 2001 From: Christina Thomas Date: Tue, 19 Sep 2017 23:53:20 -0700 Subject: [PATCH 02/11] commit for pull request on individual branch --- lab-christina/lib/server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lab-christina/lib/server.js b/lab-christina/lib/server.js index b8374b2..5c34b30 100644 --- a/lab-christina/lib/server.js +++ b/lab-christina/lib/server.js @@ -37,7 +37,7 @@ REQUESTPARSER(request) response.write('bad request'); response.end(); }); -//register routes +//register routes//add in }); module.exports = { From b8b3fe9cf7dd2431d678c758254ed01e97ed23fa Mon Sep 17 00:00:00 2001 From: Christina Thomas Date: Wed, 20 Sep 2017 17:14:07 -0700 Subject: [PATCH 03/11] vanilla server runnin/based on class demonstration --- lab-christina/.eslintignore | 5 ++ lab-christina/.eslintrc | 0 lab-christina/.eslintrc.json | 26 ++++++++++ lab-christina/index.js | 9 ++++ lab-christina/lib/request-parser.js | 29 +++++++++++ lab-christina/lib/server.js | 78 ++++++++++++++++------------- lab-christina/package.json | 20 +++++--- 7 files changed, 125 insertions(+), 42 deletions(-) delete mode 100644 lab-christina/.eslintrc create mode 100644 lab-christina/.eslintrc.json create mode 100644 lab-christina/lib/request-parser.js diff --git a/lab-christina/.eslintignore b/lab-christina/.eslintignore index e69de29..05b1cf3 100644 --- a/lab-christina/.eslintignore +++ b/lab-christina/.eslintignore @@ -0,0 +1,5 @@ +**/node_modules/* +**/vendor/* +**/*.min.js +**/coverage/* +**/build/* diff --git a/lab-christina/.eslintrc b/lab-christina/.eslintrc deleted file mode 100644 index e69de29..0000000 diff --git a/lab-christina/.eslintrc.json b/lab-christina/.eslintrc.json new file mode 100644 index 0000000..840d336 --- /dev/null +++ b/lab-christina/.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-christina/index.js b/lab-christina/index.js index e69de29..cccdb92 100644 --- a/lab-christina/index.js +++ b/lab-christina/index.js @@ -0,0 +1,9 @@ +'use strict'; + +const dotenv = require('dotenv').config(); +const server = require('./lib/server.js'); +const PORT = 4000 || process.env.PORT; + +server.start(PORT, () => { + console.log('server ::', PORT); +}); diff --git a/lab-christina/lib/request-parser.js b/lab-christina/lib/request-parser.js new file mode 100644 index 0000000..6ab2098 --- /dev/null +++ b/lab-christina/lib/request-parser.js @@ -0,0 +1,29 @@ +'use strict'; + +const url = require('url'); +const queryString = require('queryString'); + +module.exports = (request) => { + return new Promise((resolve, reject) => { + request.url = url.parse(request.url); + request.url.query = queryString.parse(request.url.query); + + if(!(request.method === 'POST' || request.method === 'PUT')) // Why not !== + return resolve(request); + + let text = ''; + // only parse the POST || PUT request bodies + request.on('data', (buffer) => { + text += buffer.toString(); + }); + + request.on('end', () => { + try { + request.body = JSON.parse(text); + resolve(request); + } catch (err) { + reject(err); + } + }); + }); +}; diff --git a/lab-christina/lib/server.js b/lab-christina/lib/server.js index 5c34b30..45877be 100644 --- a/lab-christina/lib/server.js +++ b/lab-christina/lib/server.js @@ -1,46 +1,52 @@ -const HTTP = require('http'); -const REQUESTPARSER = require('./request-parser.js'); +//node dependencies +const http = require('http'); +const requestParser = require('./request-parser.js'); +//npm dependencies +//constants +const app = http.createServer((request, response) => { + // console.log('request.method', request.method); //not necessary but useful + // console.log('request.headers', request.headers); + // console.log('request.url', request.url); -const APP = HTTP.createServer((request, response) => { - console.log('request.method', request.method); - console.log('request.headers', request.headers); - -REQUESTPARSER(request) -.then(request => { - if(request.method === 'GET' && request.url.pathname === '/'){ - response.writeHead(200, {'Content-Type': 'text/html'}); - response.write(` + //handeling route below + requestParser(request) + .then(request => { + if(request.method === 'GET' && request.url.pathname === '/'){ + response.writeHead(200, {'Content-Type': 'text/html'}); + response.write(` - Hello Universe -

Goodnight Moon

+

Hello Universe

+

Goodnight Moon

`); - response.end(); - return; - } + response.end(); + return; + } - if(request.method === 'POST' && request.url.pathname === '/echo'){ - response.writeHead(200, {'Content-Type': 'application/json'}); - response.write(JSON.stringify(request.body)); - response.end(); - return; - }; + if(request.method === 'POST' && request.url.pathname === '/echo'){ + response.writeHead(200, {'Content-Type': 'application/json'}); + response.write(JSON.stringify(request.body)); + response.end(); + return; + } - response.writeHead(404, { - 'Content-Type': 'text/plain'; - }); - response.write(`resource ${req.url.pathname} not found!`) - res.end(); -}); -.catch(err => { - console.log(err); - response.writeHead(400, {'Content-Type': 'text/plain'}); - response.write('bad request'); - response.end(); -}); + response.writeHead(404, { + 'Content-Type': 'text/plain', + }); + response.write(`resource ${request.url.pathname} not found!`); + response.end(); + }) + .catch(err => { + console.log(err); + response.writeHead(400, { + 'Content-Type': 'text/plain', + }); + response.write('bad request'); + response.end(); + }); //register routes//add in }); module.exports = { - start: (PORT, callback) => APP.listen(PORT, callback), - stop: (callback) => APP.close(callback), + start: (port, callback) => app.listen(port, callback), + stop: (callback) => app.close(callback), }; diff --git a/lab-christina/package.json b/lab-christina/package.json index 712acd0..14a78b6 100644 --- a/lab-christina/package.json +++ b/lab-christina/package.json @@ -1,15 +1,23 @@ { "name": "lab-christina", "version": "1.0.0", - "description": "''\u001b[Dlab assignment - http server'", + "description": "", "main": "index.js", "scripts": { + "start": "node index.js", + "watch": "nodemon index.js", "test": "jest -i --coverage" }, - "repository": { - "type": "git", - "url": "\u0016" - }, + "keywords": [], "author": "", - "license": "MIT" + "license": "ISC", + "dependencies": { + "dotenv": "^4.0.0", + "jest": "^21.1.0", + "querystring": "^0.2.0", + "superagent": "^3.6.0" + }, + "devDependencies": { + "nodemon": "^1.12.1" + } } From ab99a46de3b4afca9dd859f786c5216fd09f9c36 Mon Sep 17 00:00:00 2001 From: Christina Thomas Date: Wed, 20 Sep 2017 18:38:19 -0700 Subject: [PATCH 04/11] html cow added/structure of cow is innacurate --- lab-christina/__test__/server.test.js | 28 +++++++++++++++++++++++++++ lab-christina/lib/server.js | 24 +++++++---------------- 2 files changed, 35 insertions(+), 17 deletions(-) create mode 100644 lab-christina/__test__/server.test.js diff --git a/lab-christina/__test__/server.test.js b/lab-christina/__test__/server.test.js new file mode 100644 index 0000000..e483a1a --- /dev/null +++ b/lab-christina/__test__/server.test.js @@ -0,0 +1,28 @@ +'use strict'; + +const superagent = require('superagent'); +const server = require('../lib/server.js'); + +describe('GET', () => { + test('test should respond with 200'); + return superagent.post('http://localhost:4000') + .send({ + body: 'Goodnight Moon', + }) + .then(response => { + expect(response.status).toEqual(200); + expect(response.body).toEqual({body: 'Goodnight Moon'}); + }); +}); + +// test('should respond with a 400', () => { +// return superagent.post('http://localhost:4000/') +// .set({ 'Content-Type': 'application/json'}) +// .send('{') +// .then(Promise.reject) +// .catch(response => { +// expect(response.status).toEqual(400); +// console.log(response); +// expect(response.response.text).toEqual('bad request'); +// }); +// }); diff --git a/lab-christina/lib/server.js b/lab-christina/lib/server.js index 45877be..3bd0c10 100644 --- a/lab-christina/lib/server.js +++ b/lab-christina/lib/server.js @@ -3,28 +3,19 @@ const http = require('http'); const requestParser = require('./request-parser.js'); //npm dependencies //constants +let cowsay = require('cowsay'); const app = http.createServer((request, response) => { - // console.log('request.method', request.method); //not necessary but useful - // console.log('request.headers', request.headers); - // console.log('request.url', request.url); - //handeling route below requestParser(request) .then(request => { if(request.method === 'GET' && request.url.pathname === '/'){ response.writeHead(200, {'Content-Type': 'text/html'}); - response.write(` - -

Hello Universe

-

Goodnight Moon

- `); - response.end(); - return; - } - - if(request.method === 'POST' && request.url.pathname === '/echo'){ - response.writeHead(200, {'Content-Type': 'application/json'}); - response.write(JSON.stringify(request.body)); + response.write(cowsay.think({ + text: 'Result of mad cow disease',//my cow does not look like a cow. + e: '@@', + T: 'U', + wrap: false, + })); response.end(); return; } @@ -43,7 +34,6 @@ const app = http.createServer((request, response) => { response.write('bad request'); response.end(); }); -//register routes//add in }); module.exports = { From 21848b70865f1ef5308a7a8694f28e2ad1616326 Mon Sep 17 00:00:00 2001 From: Christina Thomas Date: Wed, 20 Sep 2017 19:03:24 -0700 Subject: [PATCH 05/11] cow formatting correctly --- lab-christina/__test__/server.test.js | 2 +- lab-christina/lib/server.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lab-christina/__test__/server.test.js b/lab-christina/__test__/server.test.js index e483a1a..a4cf8ad 100644 --- a/lab-christina/__test__/server.test.js +++ b/lab-christina/__test__/server.test.js @@ -3,7 +3,7 @@ const superagent = require('superagent'); const server = require('../lib/server.js'); -describe('GET', () => { +describe('POST', () => { test('test should respond with 200'); return superagent.post('http://localhost:4000') .send({ diff --git a/lab-christina/lib/server.js b/lab-christina/lib/server.js index 3bd0c10..c1c4f01 100644 --- a/lab-christina/lib/server.js +++ b/lab-christina/lib/server.js @@ -9,9 +9,9 @@ const app = http.createServer((request, response) => { requestParser(request) .then(request => { if(request.method === 'GET' && request.url.pathname === '/'){ - response.writeHead(200, {'Content-Type': 'text/html'}); + response.writeHead(200, {'Content-Type': 'JSON'}); response.write(cowsay.think({ - text: 'Result of mad cow disease',//my cow does not look like a cow. + text: 'I\'m a bird', e: '@@', T: 'U', wrap: false, From 3e22300b8aecf7fb6d30d093be2afbaffd71397c Mon Sep 17 00:00:00 2001 From: Christina Thomas Date: Wed, 20 Sep 2017 19:38:20 -0700 Subject: [PATCH 06/11] testing --- lab-christina/__test__/server.test.js | 40 +++++++++++++-------------- lab-christina/lib/server.js | 3 +- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/lab-christina/__test__/server.test.js b/lab-christina/__test__/server.test.js index a4cf8ad..7d705be 100644 --- a/lab-christina/__test__/server.test.js +++ b/lab-christina/__test__/server.test.js @@ -4,25 +4,23 @@ const superagent = require('superagent'); const server = require('../lib/server.js'); describe('POST', () => { - test('test should respond with 200'); - return superagent.post('http://localhost:4000') - .send({ - body: 'Goodnight Moon', - }) - .then(response => { - expect(response.status).toEqual(200); - expect(response.body).toEqual({body: 'Goodnight Moon'}); - }); -}); + test('should respond with a 400', () => { + return superagent.post('http://localhost:4000/') + .set({ 'Content-Type': 'application/json'}) + .send('{') + .then(Promise.reject) + .catch(response => { + expect(response.status).toEqual(400); + console.log(response); + expect(response.response.text).toEqual('bad request'); + }); + }); -// test('should respond with a 400', () => { -// return superagent.post('http://localhost:4000/') -// .set({ 'Content-Type': 'application/json'}) -// .send('{') -// .then(Promise.reject) -// .catch(response => { -// expect(response.status).toEqual(400); -// console.log(response); -// expect(response.response.text).toEqual('bad request'); -// }); -// }); + test('should respond with a 404 NOT FOUND', () => { + return superagent.post('http://localhost:4000/rando') + .catch(response => { + console.log(response); + expect(response.status).toEqual(404); + }); + }); +}); diff --git a/lab-christina/lib/server.js b/lab-christina/lib/server.js index c1c4f01..0996a31 100644 --- a/lab-christina/lib/server.js +++ b/lab-christina/lib/server.js @@ -11,7 +11,7 @@ const app = http.createServer((request, response) => { if(request.method === 'GET' && request.url.pathname === '/'){ response.writeHead(200, {'Content-Type': 'JSON'}); response.write(cowsay.think({ - text: 'I\'m a bird', + text: 'I\'m a bird',//my cow does not look like a cow. e: '@@', T: 'U', wrap: false, @@ -28,6 +28,7 @@ const app = http.createServer((request, response) => { }) .catch(err => { console.log(err); + response.writeHead(400, { 'Content-Type': 'text/plain', }); From 058b06a34164719dc55d00fa3358d955ac3c008a Mon Sep 17 00:00:00 2001 From: Christina Thomas Date: Wed, 20 Sep 2017 21:11:32 -0700 Subject: [PATCH 07/11] removal of commented code --- lab-christina/__test__/server.test.js | 2 +- lab-christina/lib/request-parser.js | 2 +- lab-christina/lib/server.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lab-christina/__test__/server.test.js b/lab-christina/__test__/server.test.js index 7d705be..81562c0 100644 --- a/lab-christina/__test__/server.test.js +++ b/lab-christina/__test__/server.test.js @@ -17,7 +17,7 @@ describe('POST', () => { }); test('should respond with a 404 NOT FOUND', () => { - return superagent.post('http://localhost:4000/rando') + return superagent.post('http://localhost:4000/') .catch(response => { console.log(response); expect(response.status).toEqual(404); diff --git a/lab-christina/lib/request-parser.js b/lab-christina/lib/request-parser.js index 6ab2098..cb5b8aa 100644 --- a/lab-christina/lib/request-parser.js +++ b/lab-christina/lib/request-parser.js @@ -8,7 +8,7 @@ module.exports = (request) => { request.url = url.parse(request.url); request.url.query = queryString.parse(request.url.query); - if(!(request.method === 'POST' || request.method === 'PUT')) // Why not !== + if(!(request.method === 'POST' || request.method === 'PUT')) return resolve(request); let text = ''; diff --git a/lab-christina/lib/server.js b/lab-christina/lib/server.js index 0996a31..a254db2 100644 --- a/lab-christina/lib/server.js +++ b/lab-christina/lib/server.js @@ -11,7 +11,7 @@ const app = http.createServer((request, response) => { if(request.method === 'GET' && request.url.pathname === '/'){ response.writeHead(200, {'Content-Type': 'JSON'}); response.write(cowsay.think({ - text: 'I\'m a bird',//my cow does not look like a cow. + text: 'I\'m a bird', e: '@@', T: 'U', wrap: false, From ca7fbade14ddc853e3c604e580e9348d8b3c606a Mon Sep 17 00:00:00 2001 From: Christina Thomas Date: Mon, 25 Sep 2017 14:40:30 -0700 Subject: [PATCH 08/11] refactoring --- lab-christina/lib/server.js | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/lab-christina/lib/server.js b/lab-christina/lib/server.js index a254db2..348c020 100644 --- a/lab-christina/lib/server.js +++ b/lab-christina/lib/server.js @@ -20,24 +20,21 @@ const app = http.createServer((request, response) => { return; } - response.writeHead(404, { - 'Content-Type': 'text/plain', - }); - response.write(`resource ${request.url.pathname} not found!`); - response.end(); - }) - .catch(err => { - console.log(err); + if(request.url.pathname !== '/'){ + response.writeHead(404, {'Content-Type': 'text/plain'}); + response.write(`resource ${request.url.pathname} not found!`); + } - response.writeHead(400, { - 'Content-Type': 'text/plain', - }); - response.write('bad request'); - response.end(); + if(request.url.pathname !== '/'){ + response.writeHead(400, {'Content-Type': 'text/plain'}); + response.write('bad request'); + response.end(); + } }); -}); -module.exports = { - start: (port, callback) => app.listen(port, callback), - stop: (callback) => app.close(callback), -}; + module.exports = { + start: (port, callback) => app.listen(port, callback), + stop: (callback) => app.close(callback), + }; + +}); From 23d33d9015df5d2406ba024dcb21b4941a2667fc Mon Sep 17 00:00:00 2001 From: Christina Thomas Date: Mon, 25 Sep 2017 14:53:34 -0700 Subject: [PATCH 09/11] .env --- lab-christina/index.js | 6 +++--- lab-christina/lib/server.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lab-christina/index.js b/lab-christina/index.js index cccdb92..901d11f 100644 --- a/lab-christina/index.js +++ b/lab-christina/index.js @@ -2,8 +2,8 @@ const dotenv = require('dotenv').config(); const server = require('./lib/server.js'); -const PORT = 4000 || process.env.PORT; +const PORT = process.env.PORT; -server.start(PORT, () => { - console.log('server ::', PORT); +server.start(process.env.PORT, () => { + console.log('server ::', process.env.PORT); }); diff --git a/lab-christina/lib/server.js b/lab-christina/lib/server.js index 348c020..f48d933 100644 --- a/lab-christina/lib/server.js +++ b/lab-christina/lib/server.js @@ -33,7 +33,7 @@ const app = http.createServer((request, response) => { }); module.exports = { - start: (port, callback) => app.listen(port, callback), + start: (PORT, callback) => app.listen(PORT, callback), stop: (callback) => app.close(callback), }; From c4319bfc3a458d817abb5caf41c887500a1e86d0 Mon Sep 17 00:00:00 2001 From: Christina Thomas Date: Mon, 25 Sep 2017 16:20:42 -0700 Subject: [PATCH 10/11] linter error blocking server fixed --- lab-christina/__test__/server.test.js | 14 +++----------- lab-christina/index.js | 6 +++--- lab-christina/lib/server.js | 18 ++++++------------ 3 files changed, 12 insertions(+), 26 deletions(-) diff --git a/lab-christina/__test__/server.test.js b/lab-christina/__test__/server.test.js index 81562c0..ca9629c 100644 --- a/lab-christina/__test__/server.test.js +++ b/lab-christina/__test__/server.test.js @@ -3,24 +3,16 @@ const superagent = require('superagent'); const server = require('../lib/server.js'); -describe('POST', () => { - test('should respond with a 400', () => { +describe('GET', () => { + test('should respond with a 404', () => { return superagent.post('http://localhost:4000/') .set({ 'Content-Type': 'application/json'}) .send('{') .then(Promise.reject) .catch(response => { - expect(response.status).toEqual(400); + expect(response.status).toEqual(404); console.log(response); expect(response.response.text).toEqual('bad request'); }); }); - - test('should respond with a 404 NOT FOUND', () => { - return superagent.post('http://localhost:4000/') - .catch(response => { - console.log(response); - expect(response.status).toEqual(404); - }); - }); }); diff --git a/lab-christina/index.js b/lab-christina/index.js index 901d11f..a6a6a94 100644 --- a/lab-christina/index.js +++ b/lab-christina/index.js @@ -2,8 +2,8 @@ const dotenv = require('dotenv').config(); const server = require('./lib/server.js'); -const PORT = process.env.PORT; +const PORT = 3000; -server.start(process.env.PORT, () => { - console.log('server ::', process.env.PORT); +server.start(PORT, () => { + console.log('server ::', PORT); }); diff --git a/lab-christina/lib/server.js b/lab-christina/lib/server.js index f48d933..d6935ba 100644 --- a/lab-christina/lib/server.js +++ b/lab-christina/lib/server.js @@ -8,7 +8,7 @@ const app = http.createServer((request, response) => { requestParser(request) .then(request => { - if(request.method === 'GET' && request.url.pathname === '/'){ + if(request.method === 'POST' && request.url.pathname === '/'){ response.writeHead(200, {'Content-Type': 'JSON'}); response.write(cowsay.think({ text: 'I\'m a bird', @@ -20,21 +20,15 @@ const app = http.createServer((request, response) => { return; } - if(request.url.pathname !== '/'){ - response.writeHead(404, {'Content-Type': 'text/plain'}); - response.write(`resource ${request.url.pathname} not found!`); - } - if(request.url.pathname !== '/'){ response.writeHead(400, {'Content-Type': 'text/plain'}); response.write('bad request'); response.end(); } }); - - module.exports = { - start: (PORT, callback) => app.listen(PORT, callback), - stop: (callback) => app.close(callback), - }; - }); + +module.exports = { + start: (PORT, callback) => app.listen(PORT, callback), + stop: (callback) => app.close(callback), +}; From c6247348e44262bc1f71b068868032247c8c5f96 Mon Sep 17 00:00:00 2001 From: Christina Thomas Date: Mon, 25 Sep 2017 20:12:07 -0700 Subject: [PATCH 11/11] html added for GET request to path '/' --- lab-christina/__test__/server.test.js | 23 ++++++++-------- lab-christina/lib/request-parser.js | 6 ++--- lab-christina/lib/server.js | 38 ++++++++++++++++++++++----- 3 files changed, 47 insertions(+), 20 deletions(-) diff --git a/lab-christina/__test__/server.test.js b/lab-christina/__test__/server.test.js index ca9629c..f8089ae 100644 --- a/lab-christina/__test__/server.test.js +++ b/lab-christina/__test__/server.test.js @@ -3,16 +3,17 @@ const superagent = require('superagent'); const server = require('../lib/server.js'); -describe('GET', () => { - test('should respond with a 404', () => { - return superagent.post('http://localhost:4000/') - .set({ 'Content-Type': 'application/json'}) - .send('{') - .then(Promise.reject) - .catch(response => { - expect(response.status).toEqual(404); - console.log(response); - expect(response.response.text).toEqual('bad request'); - }); +describe('POST', () => { + test('Should respond with a status code of 200', () => { + expect(response.status).toEqual(200); + expect(response.body).toEqual(response.body) + }); + + test('Should respond with a status code of 400', () => { + + }); + + test('Should respond with a status code of 404', () => { + }); }); diff --git a/lab-christina/lib/request-parser.js b/lab-christina/lib/request-parser.js index cb5b8aa..8ef1764 100644 --- a/lab-christina/lib/request-parser.js +++ b/lab-christina/lib/request-parser.js @@ -1,7 +1,7 @@ 'use strict'; const url = require('url'); -const queryString = require('queryString'); +const queryString = require('queryString');//lecture code module.exports = (request) => { return new Promise((resolve, reject) => { @@ -19,8 +19,8 @@ module.exports = (request) => { request.on('end', () => { try { - request.body = JSON.parse(text); - resolve(request); + request.body = JSON.parse(text);//try catch is syntax thing. .catch() is a method on a promise + resolve(request);//parsing because express does it this way } catch (err) { reject(err); } diff --git a/lab-christina/lib/server.js b/lab-christina/lib/server.js index d6935ba..c1981ff 100644 --- a/lab-christina/lib/server.js +++ b/lab-christina/lib/server.js @@ -8,8 +8,28 @@ const app = http.createServer((request, response) => { requestParser(request) .then(request => { - if(request.method === 'POST' && request.url.pathname === '/'){ + if(request.method === 'GET' && request.url.pathname === '/'){ + response.writeHead(200, {'Content-Type': 'text/html'}); + response.write(` + + + +
+ +
+
+ Using cowsay API to creat GET/POST/PUT request through and http server. + Responses are handled, parsed and tested. All I want to do is style this page. +
+ + `); + response.end(); + return; + } + + if(request.method === 'GET' && request.url.pathname === '/cowsay?text={message}'){ response.writeHead(200, {'Content-Type': 'JSON'}); + response.write(); response.write(cowsay.think({ text: 'I\'m a bird', e: '@@', @@ -20,11 +40,17 @@ const app = http.createServer((request, response) => { return; } - if(request.url.pathname !== '/'){ - response.writeHead(400, {'Content-Type': 'text/plain'}); - response.write('bad request'); - response.end(); - } + + response.writeHead(404, {'Content-Type': 'text/plain'}); + response.write('__ERROR__400__path not found'); + response.end(); + + }) + .catch(error => { + console.log(error); + response.writeHead(400, {'Content-Type': 'text/plain'}); + response.write('__ERR0R__404__bad request'); + response.end(); }); });