-
Notifications
You must be signed in to change notification settings - Fork 12
lab07- Danny #6
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
base: master
Are you sure you want to change the base?
lab07- Danny #6
Changes from all commits
cd3c965
f3c5c84
539c227
9cfce88
59b7844
99b606d
1580d79
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Vanilla HTTP 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. | ||
|
|
||
| ### Set-Up | ||
|
|
||
| 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: `<PORT>`' | ||
|
|
||
|
|
||
| ### Use | ||
|
|
||
| Making a GET request | ||
| * Run `http localhost:<PORT>/cowsay text=='<message>'` | ||
| * This will update the query text to have the cow say your <message> | ||
| * You will also receive a status code of 200. | ||
|
|
||
| * If you run `http localhost:<PORT>/cowsay` you should receive a 400 status code, and a message of 'bad request' | ||
|
|
||
| Making a POST request | ||
| * Run `http POST localhost:<PORT>/cowsay text='<message>'` | ||
| * This will update the body to have the cow say your <message> | ||
| * You will also receive a status code of 200. | ||
|
|
||
| * If you run `http POST localhost:<PORT>/cowsay` you should receive a 400 status code, and a message of 'bad request' | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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']); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| }); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| '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); //returns url object | ||
| // console.log('req querystring:', request.url.query); //returns empty object | ||
| // console.log(request.method); //returns GET method | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor, nitpick- be sure to remove commented out code before pushing to the githubs. |
||
| if(request.method === 'POST') { | ||
| 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'}); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another nitpick, I find it more convenient to use req and res over the simple premise that I don't have to type out the full words. Also, instead of request.body.text !== undefined you could just check for request.body.text |
||
| 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'}); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would get used to using the bang operator, for instance, in this case I would've done something like !request.body.text in the conditional. |
||
| 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) { | ||
| 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(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'})); | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good job! |
||
| // response.end(); | ||
| }); | ||
|
|
||
| server.listen(PORT, function() { | ||
| console.log('server live on PORT:', PORT); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good documentation!