diff --git a/lab-nathan/.eslintignore b/lab-nathan/.eslintignore new file mode 100644 index 0000000..c6abca4 --- /dev/null +++ b/lab-nathan/.eslintignore @@ -0,0 +1,6 @@ +**/node_modules/* +**/vendor/* +**/*.min.js +**/coverage/* +**/build/* +**/assets/* diff --git a/lab-nathan/.eslintrc b/lab-nathan/.eslintrc new file mode 100644 index 0000000..c8dfef7 --- /dev/null +++ b/lab-nathan/.eslintrc @@ -0,0 +1,23 @@ +{ + "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 + }, + "parserOptions": { + "ecmaFeatures": { + "modules": true, + "experimentalObjectRestSpread": true, + "impliedStrict": true + } + }, + "extends": "eslint:recommended" +} diff --git a/lab-nathan/.gitignore b/lab-nathan/.gitignore new file mode 100644 index 0000000..b232c07 --- /dev/null +++ b/lab-nathan/.gitignore @@ -0,0 +1,138 @@ +# 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 + +output diff --git a/lab-nathan/README.md b/lab-nathan/README.md new file mode 100644 index 0000000..fe47e02 --- /dev/null +++ b/lab-nathan/README.md @@ -0,0 +1,78 @@ +# Nathan's HTTP Server + +This is a simple HTTP server which can display a cow saying a custom message. There are two ways to communicate with the server, a GET request or a POST request. + +## GET +``` +/help + +/[cowsay?text=''[&cow='']] +``` + +## POST + +URL +``` +/cowsay +``` + +Body +```js +{ + "text": "", + "cow": "" +} + +``` + +## Cows +* beavis.zen +* bong +* bud-frogs +* bunny +* cheese +* cower +* daemon +* default +* doge +* dragon-and-cow +* dragon +* elephant-in-snake +* elephant +* eyes +* flaming-sheep +* ghostbusters +* goat +* head-in +* hedgehog +* hellokitty +* kiss +* kitty +* koala +* kosh +* luke-koala +* mech-and-cow +* meow +* milk +* moofasa +* moose +* mutilated +* ren +* satanic +* sheep +* skeleton +* small +* sodomized +* squirrel +* stegosaurus +* stimpy +* supermilker +* surgery +* telebears +* turkey +* turtle +* tux +* vader-koala +* vader +* whale +* www diff --git a/lab-nathan/lib/parse-body.js b/lab-nathan/lib/parse-body.js new file mode 100644 index 0000000..6714f84 --- /dev/null +++ b/lab-nathan/lib/parse-body.js @@ -0,0 +1,20 @@ +'use strict'; + +module.exports = parseBody; + +function parseBody(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); + } + }); +} \ No newline at end of file diff --git a/lab-nathan/package.json b/lab-nathan/package.json new file mode 100644 index 0000000..ec2d8a3 --- /dev/null +++ b/lab-nathan/package.json @@ -0,0 +1,16 @@ +{ + "name": "lab-nathan", + "version": "1.0.0", + "description": "", + "main": "server.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node server.js" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "cowsay": "^1.1.9" + } +} diff --git a/lab-nathan/server.js b/lab-nathan/server.js new file mode 100644 index 0000000..99212b1 --- /dev/null +++ b/lab-nathan/server.js @@ -0,0 +1,109 @@ +'use strict'; + +const http = require('http'); +const url = require('url'); +const os = require('os'); +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); + + if (request.url.pathname === '/') { + response.writeHead(200, { 'content-type': 'text/plain' }); + response.write('Welcome to Nathan\'s HTTP Server!'); + response.write(os.EOL); + response.write('GET /help for a list of commands.'); + response.write(os.EOL); + response.end(); + return; + } + + switch (request.method) { + case 'GET': + processGetRequest(request, response); + break; + case 'POST': + processPostRequest(request, response); + break; + default: + processDefaultRequest(request, response); + break; + } +}); + +function processGetRequest(request, response) { + if (request.url.pathname === '/help') { + response.writeHead(200, { 'content-type': 'text/plain' }); + response.write('Query Parameters:'); + response.write(os.EOL); + response.write('"text": What the cow will say.'); + response.write(os.EOL); + response.write('"cow": Which cow to use.'); + response.write(os.EOL); + response.write(os.EOL); + response.write('Cows:'); + response.write(os.EOL); + cowsay.list(function(err, files) { + files.forEach(function(file) { + response.write(file); + response.write(os.EOL); + }); + + response.end(); + }); + } + else if (request.url.query.text) { + let cowOptions = {}; + cowOptions.text = request.url.query.text; + + if (request.url.query.cow) { + cowOptions.f = request.url.query.cow; + } + + response.writeHead(200, { 'content-type': 'text/plain' }); + response.write(cowsay.say(cowOptions)); + response.end(); + } + else { + writeBadRequest(request, response); + response.end(); + } +} + +function processPostRequest(request, response) { + parseBody(request, function(err) { + if (err || !request.body.text) { + writeBadRequest(request, response); + } else { + let cowOptions = {}; + cowOptions.text = request.body.text; + + if (request.body.cow) { + cowOptions.f = request.body.cow; + } + response.writeHead(200, { 'content-type': 'text/plain' }); + response.write(cowsay.say(cowOptions)); + } + + response.end(); + }); +} + +function writeBadRequest(request, response) { + response.writeHead(400, { 'content-type': 'text/plain' }); + response.write(cowsay.say({text: 'bad request'})); +} + +function processDefaultRequest(request, response) { + response.writeHead(400, { 'content-type': 'text/plain' }); + response.write('Only GET and POST requests are currently supported.'); + response.end(); +} + +server.listen(PORT, function() { + console.log('Listening on port:', PORT); +}); \ No newline at end of file