From cd3c965493e843775a2012fc4f15ec3d871cc763 Mon Sep 17 00:00:00 2001 From: Daniel Becker Date: Tue, 13 Dec 2016 15:56:32 -0800 Subject: [PATCH 1/7] initial set-up complete, all files added --- .eslintrc | 21 ++++++++++ .gitignore | 102 ++++++++++++++++++++++++++++++++++++++++++++++ README.md | 0 gulpfile.js | 23 +++++++++++ lib/parse-body.js | 0 package.json | 24 +++++++++++ server.js | 0 7 files changed, 170 insertions(+) create mode 100644 .eslintrc create mode 100644 .gitignore create mode 100644 README.md create mode 100644 gulpfile.js create mode 100644 lib/parse-body.js create mode 100644 package.json create mode 100644 server.js 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..5eeb33e --- /dev/null +++ b/.gitignore @@ -0,0 +1,102 @@ + +# Created by https://www.gitignore.io/api/macos,node,vim,vim + +### 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* +node_modules + +# 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 + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules +jspm_packages + +# 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 + + + +### Vim ### +# swap +[._]*.s[a-w][a-z] +[._]s[a-w][a-z] +# session +Session.vim +# temporary +.netrwhist +*~ +# auto-generated tag files +tags + + +### Vim ### +# swap +# session +# temporary +# auto-generated tag files + +### project specific ### +temp +img/test.bmp diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..9bc33f9 --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,23 @@ +'use strict'; + +const gulp = require('gulp'); +const eslint = require('gulp-eslint'); +const mocha = require('gulp-mocha'); + +gulp.task('test', function() { + gulp.src('./test/*-test.js', { read: false }) + .pipe(mocha({ reporter: 'spec' })); +}); + +gulp.task('lint', function() { + return gulp.src(['**/*.js', '!node_modules']) + .pipe(eslint()) + .pipe(eslint.format()) + .pipe(eslint.failAfterError()); +}); + +gulp.task('dev', function() { + gulp.watch(['**/*.js', '!node_modules'], ['lint', 'task']); +}); + +gulp.task('default', ['dev']); diff --git a/lib/parse-body.js b/lib/parse-body.js new file mode 100644 index 0000000..e69de29 diff --git a/package.json b/package.json new file mode 100644 index 0000000..ed3da49 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "07-vanilla-http-server", + "version": "1.0.0", + "description": "", + "main": "gulpfile.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node server.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/dbecker4130/07-vanilla-http-server.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/dbecker4130/07-vanilla-http-server/issues" + }, + "homepage": "https://github.com/dbecker4130/07-vanilla-http-server#readme", + "dependencies": { + "cowsay": "^1.1.9" + } +} diff --git a/server.js b/server.js new file mode 100644 index 0000000..e69de29 From f3c5c84e13a213c8770bed272274b8506ca26063 Mon Sep 17 00:00:00 2001 From: Daniel Becker Date: Tue, 13 Dec 2016 16:04:03 -0800 Subject: [PATCH 2/7] parse-body.js module complete --- lib/parse-body.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/parse-body.js b/lib/parse-body.js index e69de29..30fa25e 100644 --- a/lib/parse-body.js +++ b/lib/parse-body.js @@ -0,0 +1,18 @@ +'use strict'; + +module.exports = function(request, callback) { + request.body = ''; + + request.on('data', function(data) { + request.body += data.toString(); + }); + + request.on('end', function() { + try { + request.body = JSON.parse(request.body); + callback(null, request.body); + } catch (err) { + callback(err); + } + }); +}; From 539c2277304a3f46c936ea859b6e5c9232c80d8a Mon Sep 17 00:00:00 2001 From: Daniel Becker Date: Tue, 13 Dec 2016 16:34:10 -0800 Subject: [PATCH 3/7] request and response cycle works --- server.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/server.js b/server.js index e69de29..acb8610 100644 --- a/server.js +++ b/server.js @@ -0,0 +1,29 @@ +'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(function(request, response) { + request.url = url.parse(request.url); + request.url.query = querystring.parse(request.url.query); + + console.log('req url:', request.url); + console.log('req querystring:', request.url.query); + console.log(request.method); + + if(request.method === 'POST') { + parseBody(request, function(err) { + if (err) console.log(err); + console.log('POST request body:', request.body); + }); + } + response.end(); +}); + +server.listen(PORT, function() { + console.log('server live on:', PORT); +}); From 9cfce88eee096b99d0f7b53163c874b3a5671065 Mon Sep 17 00:00:00 2001 From: Daniel Becker Date: Tue, 13 Dec 2016 16:50:39 -0800 Subject: [PATCH 4/7] cow displays in terminal when running http localhost:4130/cowsay --- server.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/server.js b/server.js index acb8610..f94587e 100644 --- a/server.js +++ b/server.js @@ -7,20 +7,25 @@ const cowsay = require('cowsay'); const parseBody = require('./lib/parse-body.js'); const PORT = process.env.PORT || 3000; -const server = http.createServer(function(request, response) { - request.url = url.parse(request.url); - request.url.query = querystring.parse(request.url.query); +const server = http.createServer(function(req, response) { + req.url = url.parse(req.url); + req.url.query = querystring.parse(req.url.query); - console.log('req url:', request.url); - console.log('req querystring:', request.url.query); - console.log(request.method); + // console.log('req url:', request.url); //returns url object + // console.log('req querystring:', request.url.query); //returns empty object + // console.log(request.method); //returns GET method - if(request.method === 'POST') { - parseBody(request, function(err) { + if(req.method === 'POST') { + parseBody(req, function(err) { if (err) console.log(err); - console.log('POST request body:', request.body); + console.log('POST request body:', req.body); }); } + + if(req.method === 'GET' && req.url.pathname === '/cowsay') { + response.write(cowsay.say({text: 'the cow says hello'})); + response.end(); + } response.end(); }); From 59b7844a73f992bf3b927d6044c11d0d9dd722d9 Mon Sep 17 00:00:00 2001 From: Daniel Becker Date: Tue, 13 Dec 2016 18:14:50 -0800 Subject: [PATCH 5/7] get requests work with text=message and responds with bad request on cowsay/ --- README.md | 7 +++++++ server.js | 31 ++++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index e69de29..5740a68 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,7 @@ +# Vanilla HTTP server + +This server.... + +# use + +Run `http localhost:/cowsay` diff --git a/server.js b/server.js index f94587e..b155a62 100644 --- a/server.js +++ b/server.js @@ -7,25 +7,38 @@ const cowsay = require('cowsay'); const parseBody = require('./lib/parse-body.js'); const PORT = process.env.PORT || 3000; -const server = http.createServer(function(req, response) { - req.url = url.parse(req.url); - req.url.query = querystring.parse(req.url.query); +const server = http.createServer(function(request, response) { + request.url = url.parse(request.url); + request.url.query = querystring.parse(request.url.query); // console.log('req url:', request.url); //returns url object // console.log('req querystring:', request.url.query); //returns empty object // console.log(request.method); //returns GET method - if(req.method === 'POST') { - parseBody(req, function(err) { + if(request.method === 'POST') { + parseBody(request, function(err) { if (err) console.log(err); - console.log('POST request body:', req.body); + console.log('POST request body:', request.body); }); } + if(request.url.pathname === '/') { + response.writeHead(200, {'Content-Type': 'text/plain'}); + response.end('what up from my server'); + } + + if(request.method === 'GET' && request.url.pathname === '/cowsay' && request.url.query.text !== undefined) { + // if(request.method === 'GET' && request.url.pathname === '/cowsay') { + + response.writeHead(200, {'Content-Type': 'text/plain'}); + // response.write(cowsay.say({text: 'the cow says hello'})); + response.end(cowsay.say({text: request.url.query.text.trim()})); - if(req.method === 'GET' && req.url.pathname === '/cowsay') { - response.write(cowsay.say({text: 'the cow says hello'})); - response.end(); } + if(request.method === 'GET' && request.url.pathname === '/cowsay' && request.url.query.text === undefined) { + response.writeHead(400, {'Content-Type': 'text/plain'}); + response.end(cowsay.say({text: 'bad request'})); + } + response.end(); }); From 99b606d883b43c98fdd9881a779e102faa8b9e56 Mon Sep 17 00:00:00 2001 From: Daniel Becker Date: Wed, 14 Dec 2016 10:58:31 -0800 Subject: [PATCH 6/7] rough README complete, GET and POST requests both repsond correctly --- README.md | 27 ++++++++++++++++++++++++--- server.js | 15 +++++++++++---- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 5740a68..2fb8ffb 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,28 @@ # Vanilla HTTP server -This server.... +This server accesses the cowsay API, and allows the user to make GET and POST requests. On response, the user should receive a cow that displays a message of the users choice. -# use +### Set-Up -Run `http localhost:/cowsay` +In your Terminal, run `brew install httpie`. NOTE: you must have homebrew installed to do this. `/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"` + +Run `npm i` to install proper dependencies. You should receive cowsay in your package.json file. + +Run `node server.js` to start your server. You will receive a response of 'server live on PORT: ``' + + +### Use + +Making a GET request +Run `http localhost:/cowsay text==''` +This will update the query text to have the cow say your +You will also receive a status code of 200. + +If you run `http localhost:/cowsay` you should receive a 400 status code, and a message of 'bad request' + +Making a POST request +Run `http POST localhost:/cowsay text=''` +This will update the body to have the cow say your +You will also receive a status code of 200. + +If you run `http POST localhost:/cowsay` you should receive a 400 status code, and a message of 'bad request' diff --git a/server.js b/server.js index b155a62..6a6ed51 100644 --- a/server.js +++ b/server.js @@ -19,16 +19,23 @@ const server = http.createServer(function(request, response) { parseBody(request, function(err) { if (err) console.log(err); console.log('POST request body:', request.body); + if(request.url.pathname === '/cowsay' && request.body.text !== undefined) { + response.writeHead(200, {'Content-Type': 'text/plain'}); + response.end(cowsay.say({text: request.body.text.trim()})); + } + if(request.url.pathname === '/cowsay' && request.body.text === undefined) { + response.writeHead(400, {'Content-Type': 'text/plain'}); + response.end(cowsay.say({text: 'bad request'})); + } }); } + if(request.url.pathname === '/') { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('what up from my server'); } if(request.method === 'GET' && request.url.pathname === '/cowsay' && request.url.query.text !== undefined) { - // if(request.method === 'GET' && request.url.pathname === '/cowsay') { - response.writeHead(200, {'Content-Type': 'text/plain'}); // response.write(cowsay.say({text: 'the cow says hello'})); response.end(cowsay.say({text: request.url.query.text.trim()})); @@ -39,9 +46,9 @@ const server = http.createServer(function(request, response) { response.end(cowsay.say({text: 'bad request'})); } - response.end(); + // response.end(); }); server.listen(PORT, function() { - console.log('server live on:', PORT); + console.log('server live on PORT:', PORT); }); From 1580d790349d3204519a5f1c4979baad671360e9 Mon Sep 17 00:00:00 2001 From: Daniel Becker Date: Wed, 14 Dec 2016 11:02:50 -0800 Subject: [PATCH 7/7] updating README --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2fb8ffb..ec155bc 100644 --- a/README.md +++ b/README.md @@ -14,15 +14,15 @@ Run `node server.js` to start your server. You will receive a response of 'serve ### Use Making a GET request -Run `http localhost:/cowsay text==''` -This will update the query text to have the cow say your -You will also receive a status code of 200. +* Run `http localhost:/cowsay text==''` +* This will update the query text to have the cow say your +* You will also receive a status code of 200. -If you run `http localhost:/cowsay` you should receive a 400 status code, and a message of 'bad request' +* If you run `http localhost:/cowsay` you should receive a 400 status code, and a message of 'bad request' Making a POST request -Run `http POST localhost:/cowsay text=''` -This will update the body to have the cow say your -You will also receive a status code of 200. +* Run `http POST localhost:/cowsay text=''` +* This will update the body to have the cow say your +* You will also receive a status code of 200. -If you run `http POST localhost:/cowsay` you should receive a 400 status code, and a message of 'bad request' +* If you run `http POST localhost:/cowsay` you should receive a 400 status code, and a message of 'bad request'