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..ec155bc --- /dev/null +++ b/README.md @@ -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: ``' + + +### 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/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..30fa25e --- /dev/null +++ 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); + } + }); +}; 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..6a6ed51 --- /dev/null +++ b/server.js @@ -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 + + 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'}); + 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) { + 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'})); + } + + // response.end(); +}); + +server.listen(PORT, function() { + console.log('server live on PORT:', PORT); +});