From 150a79be699862b0161e860f3f8beedaf36b7dee Mon Sep 17 00:00:00 2001 From: Jamesbillard12 Date: Tue, 25 Jul 2017 15:18:35 -0700 Subject: [PATCH 1/9] Created parcer module file --- .eslintignore | 5 ++ .eslintrc | 21 +++++++ .gitignore | 136 ++++++++++++++++++++++++++++++++++++++++++++++ lib/parce-body.js | 18 ++++++ package.json | 23 ++++++++ server.js | 15 +++++ 6 files changed, 218 insertions(+) create mode 100644 .eslintignore create mode 100644 .eslintrc create mode 100644 .gitignore create mode 100644 lib/parce-body.js create mode 100644 package.json create mode 100644 server.js diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..05b1cf3 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,5 @@ +**/node_modules/* +**/vendor/* +**/*.min.js +**/coverage/* +**/build/* diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..8dc6807 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,21 @@ +{ + "rules": { + "no-console": "off", + "indent": [ "error", 2 ], + "quotes": [ "error", "single" ], + "semi": ["error", "always"], + "linebreak-style": [ "error", "unix" ] + }, + "env": { + "es6": true, + "node": true, + "mocha": true, + "jasmine": true + }, + "ecmaFeatures": { + "modules": true, + "experimentalObjectRestSpread": true, + "impliedStrict": true + }, + "extends": "eslint:recommended" +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..345130c --- /dev/null +++ b/.gitignore @@ -0,0 +1,136 @@ +# Created by https://www.gitignore.io/api/osx,vim,node,macos,windows + +### macOS ### +*.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 + +### 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 ### + +# Icon must end with two \r + +# Thumbnails + +# Files that might appear in the root of a volume + +# Directories potentially created on remote AFP share + +### 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/osx,vim,node,macos,windows diff --git a/lib/parce-body.js b/lib/parce-body.js new file mode 100644 index 0000000..cba89b9 --- /dev/null +++ b/lib/parce-body.js @@ -0,0 +1,18 @@ +'use strict'; + +module.exports = function(req, callback) { + req.body = ''; + + req.on('data', function(data){ + req.body += data.toString(); + }); + + req.on('end', function(){ + try { + req.body = JSON.parse(req.body); + callback(null, req.body); + } catch (err) { + callback(err); + } + }); +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..74980a9 --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "07-http_servers", + "version": "1.0.0", + "description": "![cf](https://i.imgur.com/7v5ASc8.png) Lab 07: Vanilla HTTP Server ======", + "main": "server.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node server.js" + }, + "repository": { + "start": "node server.js" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/Jamesbillard12/07-http_servers/issues" + }, + "homepage": "https://github.com/Jamesbillard12/07-http_servers#readme", + "dependencies": { + "cowsay": "^1.1.9" + } +} diff --git a/server.js b/server.js new file mode 100644 index 0000000..5e77851 --- /dev/null +++ b/server.js @@ -0,0 +1,15 @@ +'use strict'; + +const http = require('http'); +const url = require('url'); +const querystring = require('querystring'); +const cowsay = require('cowsay'); +const parseBody = require('./lib/parse-body.js'); +const PORT = process.env.PORT || 3000; + +const server = http.createServer(); + + +server.listen(PORT, function(){ + console.log('server', PORT); +}); From b9328767f6e8ff17fd64b3663d772437799a306c Mon Sep 17 00:00:00 2001 From: Jamesbillard12 Date: Tue, 25 Jul 2017 15:37:41 -0700 Subject: [PATCH 2/9] everything from demo typed in --- server.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/server.js b/server.js index 5e77851..356e5ab 100644 --- a/server.js +++ b/server.js @@ -7,7 +7,23 @@ const cowsay = require('cowsay'); const parseBody = require('./lib/parse-body.js'); const PORT = process.env.PORT || 3000; -const server = http.createServer(); +const server = http.createServer(function(req, res){ + req.url = url.parce(req.url); + req.url.query = querystring.parse(req.url.query); + + if(req.method === 'POST') { + parseBody(req, function(err){ + if(err) return console.log(err); + console.log('POST request body: req.body'); + }); + } + + if(req.method === 'GET' && req.url.pathname === '/cowsay'){ + let params = req.url.query; + console.log('my querystring paramaters:', params); + res.write(cowsay.say({text: params.text})); + } +}); server.listen(PORT, function(){ From 4b1f19c5816b2f33447ebdcc7a6fa34cac8e8e17 Mon Sep 17 00:00:00 2001 From: Jamesbillard12 Date: Tue, 25 Jul 2017 16:14:50 -0700 Subject: [PATCH 3/9] cowsay is workingish --- lib/{parce-body.js => parse-body.js} | 4 ++-- server.js | 24 +++++++++++++----------- 2 files changed, 15 insertions(+), 13 deletions(-) rename lib/{parce-body.js => parse-body.js} (80%) diff --git a/lib/parce-body.js b/lib/parse-body.js similarity index 80% rename from lib/parce-body.js rename to lib/parse-body.js index cba89b9..d5b5766 100644 --- a/lib/parce-body.js +++ b/lib/parse-body.js @@ -3,11 +3,11 @@ module.exports = function(req, callback) { req.body = ''; - req.on('data', function(data){ + req.on('data', function(data) { req.body += data.toString(); }); - req.on('end', function(){ + req.on('end', function() { try { req.body = JSON.parse(req.body); callback(null, req.body); diff --git a/server.js b/server.js index 356e5ab..20b1bda 100644 --- a/server.js +++ b/server.js @@ -7,25 +7,27 @@ const cowsay = require('cowsay'); const parseBody = require('./lib/parse-body.js'); const PORT = process.env.PORT || 3000; -const server = http.createServer(function(req, res){ - req.url = url.parce(req.url); +const server = http.createServer(function(req, res) { + req.url = url.parse(req.url); req.url.query = querystring.parse(req.url.query); - if(req.method === 'POST') { - parseBody(req, function(err){ - if(err) return console.log(err); - console.log('POST request body: req.body'); + if (req.method === 'POST') { + parseBody(req, function(err) { + if (err) return console.error(err); + console.log('POST request body:', req.body); }); } - if(req.method === 'GET' && req.url.pathname === '/cowsay'){ + if (req.method === 'GET' && req.url.pathname === '/cowsay') { let params = req.url.query; console.log('my querystring paramaters:', params); - res.write(cowsay.say({text: params.text})); + res.write(cowsay.say({ text: params.text })); + res.end(); } -}); + res.end(); +}); -server.listen(PORT, function(){ - console.log('server', PORT); +server.listen(PORT, function() { + console.log('server up:', PORT); }); From e47d938d706d68f7804505903ca30e325d53c226 Mon Sep 17 00:00:00 2001 From: Jamesbillard12 Date: Tue, 25 Jul 2017 17:38:31 -0700 Subject: [PATCH 4/9] cowsay is done --- server.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/server.js b/server.js index 20b1bda..40d0f56 100644 --- a/server.js +++ b/server.js @@ -18,11 +18,26 @@ const server = http.createServer(function(req, res) { }); } + if (req.method === 'GET' && req.url.pathname === '/cowsay') { let params = req.url.query; - console.log('my querystring paramaters:', params); - res.write(cowsay.say({ text: params.text })); - res.end(); + if (!params.text) { + res.statusCode = 400; + res.write(cowsay.say({ text: 'bad request' })); + }else{ + res.statusCode = 200; + res.write(cowsay.say({ text: params.text })); + res.end(); + } + } + + if (req.method === 'GET' && req.url.pathname === '/') { + console.log(res); + res.writeHead(200,{ + 'content-Type': 'text/plain' + }); + console.log(res); + res.end('hello from my server!'); } res.end(); From 0908960e6520c7de8ba88fa0eff3a0596b340668 Mon Sep 17 00:00:00 2001 From: Jamesbillard12 Date: Tue, 25 Jul 2017 18:31:05 -0700 Subject: [PATCH 5/9] finished assignment --- server.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/server.js b/server.js index 40d0f56..fe08a31 100644 --- a/server.js +++ b/server.js @@ -11,10 +11,19 @@ const server = http.createServer(function(req, res) { req.url = url.parse(req.url); req.url.query = querystring.parse(req.url.query); - if (req.method === 'POST') { + if (req.method === 'POST' && req.url.pathname === '/cowsay') { parseBody(req, function(err) { + console.log(req.body.text); if (err) return console.error(err); - console.log('POST request body:', req.body); + if (!req.body) { + req.statusCode = 400; + res.write(cowsay.say({ text: 'bad request' })); + res.end(); + }else{ + req.statusCode = 200; + res.write(cowsay.say({ text: req.body['text'] })); + res.end(); + } }); } @@ -24,6 +33,7 @@ const server = http.createServer(function(req, res) { if (!params.text) { res.statusCode = 400; res.write(cowsay.say({ text: 'bad request' })); + res.end(); }else{ res.statusCode = 200; res.write(cowsay.say({ text: params.text })); @@ -32,15 +42,11 @@ const server = http.createServer(function(req, res) { } if (req.method === 'GET' && req.url.pathname === '/') { - console.log(res); res.writeHead(200,{ 'content-Type': 'text/plain' }); - console.log(res); res.end('hello from my server!'); } - - res.end(); }); server.listen(PORT, function() { From 39ff418bdc7a87781b5c916389490c51b7603d97 Mon Sep 17 00:00:00 2001 From: Jamesbillard12 Date: Tue, 25 Jul 2017 21:25:32 -0700 Subject: [PATCH 6/9] finished the assignment --- lib/parse-body.js | 11 ++++++++--- server.js | 37 +++++++++++++++++++++++++++---------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/lib/parse-body.js b/lib/parse-body.js index d5b5766..431916e 100644 --- a/lib/parse-body.js +++ b/lib/parse-body.js @@ -1,15 +1,20 @@ 'use strict'; module.exports = function(req, callback) { - req.body = ''; + let tempBody = ''; req.on('data', function(data) { - req.body += data.toString(); + tempBody += data.toString(); }); req.on('end', function() { + console.log(tempBody) + if(!tempBody){ + return callback(new Error('empty body')); + } try { - req.body = JSON.parse(req.body); + let parse = JSON.parse(tempBody); + req.body = parse; callback(null, req.body); } catch (err) { callback(err); diff --git a/server.js b/server.js index fe08a31..cd829da 100644 --- a/server.js +++ b/server.js @@ -11,29 +11,43 @@ const server = http.createServer(function(req, res) { req.url = url.parse(req.url); req.url.query = querystring.parse(req.url.query); - if (req.method === 'POST' && req.url.pathname === '/cowsay') { + if (req.method === 'POST' && req.url.pathname ==='/') { parseBody(req, function(err) { - console.log(req.body.text); if (err) return console.error(err); - if (!req.body) { - req.statusCode = 400; - res.write(cowsay.say({ text: 'bad request' })); + console.log('POST request body:', req.body); + }); + } + + if (req.method === 'POST' && req.url.pathname === '/cowsay') { + // let params = req.url.query; + parseBody(req, function(err) { + console.log('hello'); + if(err) { + console.log('bye'); + res.writeHead(400, { + 'Content-type': 'text-plain' + }); + res.write(cowsay.say({ + text: 'bad request' + })); res.end(); - }else{ - req.statusCode = 200; - res.write(cowsay.say({ text: req.body['text'] })); + } else { + res.writeHead(200, { + 'Content-type': 'text-plain' + }); + res.write(cowsay.say({ + text: req.body.text + })); res.end(); } }); } - if (req.method === 'GET' && req.url.pathname === '/cowsay') { let params = req.url.query; if (!params.text) { res.statusCode = 400; res.write(cowsay.say({ text: 'bad request' })); - res.end(); }else{ res.statusCode = 200; res.write(cowsay.say({ text: params.text })); @@ -42,11 +56,14 @@ const server = http.createServer(function(req, res) { } if (req.method === 'GET' && req.url.pathname === '/') { + console.log(res); res.writeHead(200,{ 'content-Type': 'text/plain' }); + console.log(res); res.end('hello from my server!'); } + }); server.listen(PORT, function() { From 2ec85c8ff8d0550885e5afc946c551287ad28929 Mon Sep 17 00:00:00 2001 From: Jamesbillard12 Date: Tue, 25 Jul 2017 21:31:17 -0700 Subject: [PATCH 7/9] finished assignment --- lib/parse-body.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/parse-body.js b/lib/parse-body.js index 431916e..3297953 100644 --- a/lib/parse-body.js +++ b/lib/parse-body.js @@ -8,7 +8,6 @@ module.exports = function(req, callback) { }); req.on('end', function() { - console.log(tempBody) if(!tempBody){ return callback(new Error('empty body')); } From 6a947447572f164a1a9c1619dc1f716b21f7c90e Mon Sep 17 00:00:00 2001 From: Jamesbillard12 Date: Wed, 26 Jul 2017 09:28:12 -0700 Subject: [PATCH 8/9] finished --- server.js | 1 - 1 file changed, 1 deletion(-) diff --git a/server.js b/server.js index cd829da..52702ad 100644 --- a/server.js +++ b/server.js @@ -19,7 +19,6 @@ const server = http.createServer(function(req, res) { } if (req.method === 'POST' && req.url.pathname === '/cowsay') { - // let params = req.url.query; parseBody(req, function(err) { console.log('hello'); if(err) { From e985e4d0650157be706145ea9b5cb376189e7d53 Mon Sep 17 00:00:00 2001 From: Jamesbillard12 Date: Wed, 26 Jul 2017 11:20:51 -0700 Subject: [PATCH 9/9] still need to complete readme --- server.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/server.js b/server.js index 52702ad..426e82b 100644 --- a/server.js +++ b/server.js @@ -27,7 +27,7 @@ const server = http.createServer(function(req, res) { 'Content-type': 'text-plain' }); res.write(cowsay.say({ - text: 'bad request' + text: 'oops, you mooooootilated that request!', f: 'mutilated' })); res.end(); } else { @@ -46,7 +46,10 @@ const server = http.createServer(function(req, res) { let params = req.url.query; if (!params.text) { res.statusCode = 400; - res.write(cowsay.say({ text: 'bad request' })); + res.write(cowsay.say({ + text: 'oops, you mooooootilated that request!', f: 'mutilated' + })); + res.end(); }else{ res.statusCode = 200; res.write(cowsay.say({ text: params.text }));