From 310c23782425f1e0802f78d26911de405635dc4c Mon Sep 17 00:00:00 2001 From: Kyler Dotson Date: Wed, 14 Dec 2016 10:36:21 -0800 Subject: [PATCH 1/2] done with lab --- lab-kyler/.eslintrc | 21 ++++++ lab-kyler/.gitignore | 126 ++++++++++++++++++++++++++++++++++++ lab-kyler/gulpfile.js | 20 ++++++ lab-kyler/lib/parse-body.js | 19 ++++++ lab-kyler/package.json | 16 +++++ lab-kyler/readme.md | 15 +++++ lab-kyler/server.js | 59 +++++++++++++++++ 7 files changed, 276 insertions(+) create mode 100644 lab-kyler/.eslintrc create mode 100644 lab-kyler/.gitignore create mode 100644 lab-kyler/gulpfile.js create mode 100644 lab-kyler/lib/parse-body.js create mode 100644 lab-kyler/package.json create mode 100644 lab-kyler/readme.md create mode 100644 lab-kyler/server.js diff --git a/lab-kyler/.eslintrc b/lab-kyler/.eslintrc new file mode 100644 index 0000000..8dc6807 --- /dev/null +++ b/lab-kyler/.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/lab-kyler/.gitignore b/lab-kyler/.gitignore new file mode 100644 index 0000000..91142a2 --- /dev/null +++ b/lab-kyler/.gitignore @@ -0,0 +1,126 @@ +Created by https://www.gitignore.io/api/macos,node,vim,windows,linux + +### 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 ### +node_modules +# Logs +logs +*.log +npm-debug.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 + +# 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 + + +### Windows ### +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msm +*.msp + +# Windows shortcuts +*.lnk + + +### Linux ### + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* diff --git a/lab-kyler/gulpfile.js b/lab-kyler/gulpfile.js new file mode 100644 index 0000000..28806cb --- /dev/null +++ b/lab-kyler/gulpfile.js @@ -0,0 +1,20 @@ +'use strict'; + +const gulp = require('gulp'); +//const fs = require('fs'); +const eslint = require('gulp-eslint'); +const mocha = require('gulp-mocha'); + +gulp.task('test', function() { + return 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('default', ['lint', 'test']); diff --git a/lab-kyler/lib/parse-body.js b/lab-kyler/lib/parse-body.js new file mode 100644 index 0000000..d9865b2 --- /dev/null +++ b/lab-kyler/lib/parse-body.js @@ -0,0 +1,19 @@ +'use strict'; + +module.exports = function(req, cb) { //if you just export a function, you can use your var x = require(y) directly + req.body = ''; + + req.on('data', (data) => { //concat chunks as they come in + req.body += data.toString(); + }); + + req.on('end', () => { //time to parse the string - reverse of stringify + try { + req.body = JSON.parse(req.body); + cb(null, req.body); + } + catch (err) { + cb(err); + } + }); +}; diff --git a/lab-kyler/package.json b/lab-kyler/package.json new file mode 100644 index 0000000..83d2d5a --- /dev/null +++ b/lab-kyler/package.json @@ -0,0 +1,16 @@ +{ + "name": "lab07-kyler", + "version": "1.0.0", + "description": "", + "main": "server.js", + "scripts": { + "start": "node server.js", + "test": "gulp" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "cowsay": "^1.1.9" + } +} diff --git a/lab-kyler/readme.md b/lab-kyler/readme.md new file mode 100644 index 0000000..5885b13 --- /dev/null +++ b/lab-kyler/readme.md @@ -0,0 +1,15 @@ +#MenagerieServer by Kyler Dotson +It's an http server on `$PORT` containing many exotic and stoned animals +##How to use +###*Start server with `npm start` +*A welcome message: + *`http localhost:/` +*A custom message: + *`http localhost:/cowsay?text=` or + *`http POST localhost:/cowsay text=` +*A custom message from a custom animal: + *`http localhost:2000/cowsay?text=&cow=` or + *`http POST localhost:/cowsay?cow= text=` +*An error: + *`http localhost:2000/cowsay` or + *`http POST localhost:2000/cowsay` diff --git a/lab-kyler/server.js b/lab-kyler/server.js new file mode 100644 index 0000000..a7c7432 --- /dev/null +++ b/lab-kyler/server.js @@ -0,0 +1,59 @@ +'use strict'; + +const http = require('http'); +const url = require('url'); +const querystring = require('querystring'); //parses query strings +const cowsay = require('cowsay'); +const parseBody = require('./lib/parse-body.js'); +const PORT = process.env.PORT || 2001; + +const server = http.createServer(function(req, res) { + req.url = url.parse(req.url); + req.url.query = querystring.parse(req.url.query, true); + var cow = req.url.query.cow ? req.url.query.cow : 'bong'; + + console.log('request received, method:', req.method, 'animal:', cow); + + if (req.url.pathname === '/cowsay') { + res.setHeader('Content-Type', 'text/plain'); + + if (req.method === 'POST') { + parseBody(req, function(err) { + if(err) console.log('parseBody error:', err); + if (req.body.text) { + res.statusCode = 200; + res.write(cowsay.say({text: req.body.text, f:cow})); + } + else { + res.statusCode = 400; + res.write(cowsay.say({text: 'You made a moooooostake'})); + } + res.end(); + }); + } + + if (req.method === 'GET') { + if(req.url.query.text) { + res.statusCode = 200; + res.write(cowsay.say({text: req.url.query.text, f:cow})); + res.end(); + } + else { + res.statusCode = 400; + res.write(cowsay.say({text: 'I\'m a cow, not a mind reader.'})); + res.end(); + } + } + } + + if (req.url.pathname === '/') { + res.setHeader('Content-Type', 'text/plain'); + res.statusCode = 200; + res.write('hello from my server!'); + res.end(); + } +}); + +server.listen(PORT, () => { + console.log('server up:', PORT); +}); From 9459671a118bb2a74c905f676b198077743ed9b2 Mon Sep 17 00:00:00 2001 From: bazacko Date: Wed, 14 Dec 2016 10:39:00 -0800 Subject: [PATCH 2/2] markdown, how does it work --- lab-kyler/readme.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lab-kyler/readme.md b/lab-kyler/readme.md index 5885b13..3f0e8cb 100644 --- a/lab-kyler/readme.md +++ b/lab-kyler/readme.md @@ -1,15 +1,15 @@ -#MenagerieServer by Kyler Dotson +# MenagerieServer by Kyler Dotson It's an http server on `$PORT` containing many exotic and stoned animals -##How to use -###*Start server with `npm start` -*A welcome message: - *`http localhost:/` -*A custom message: - *`http localhost:/cowsay?text=` or - *`http POST localhost:/cowsay text=` -*A custom message from a custom animal: - *`http localhost:2000/cowsay?text=&cow=` or - *`http POST localhost:/cowsay?cow= text=` -*An error: - *`http localhost:2000/cowsay` or - *`http POST localhost:2000/cowsay` +## How to use +Start server with `npm start` +* A welcome message: + * `http localhost:/` +* A custom message: + * `http localhost:/cowsay?text=` or + * `http POST localhost:/cowsay text=` +* A custom message from a custom animal: + * `http localhost:2000/cowsay?text=&cow=` or + * `http POST localhost:/cowsay?cow= text=` +* An error: + * `http localhost:2000/cowsay` or + * `http POST localhost:2000/cowsay`