-
+
- Click the cowsay link to get the cow's default message. +
- If you want the cow to say anything else, add "?text=" + followed by your message to the end of the URL and refresh. +
diff --git a/lab-katherine/.eslintignore b/lab-katherine/.eslintignore new file mode 100644 index 0000000..05b1cf3 --- /dev/null +++ b/lab-katherine/.eslintignore @@ -0,0 +1,5 @@ +**/node_modules/* +**/vendor/* +**/*.min.js +**/coverage/* +**/build/* diff --git a/lab-katherine/.eslintrc.json b/lab-katherine/.eslintrc.json new file mode 100644 index 0000000..840d336 --- /dev/null +++ b/lab-katherine/.eslintrc.json @@ -0,0 +1,26 @@ +{ + "env": { + "browser": true, + "node": true, + "commonjs": true, + "jest": true, + "es6": true + }, + "globals": { + "err": true, + "req": true, + "res": true, + "next": true + }, + "extends": "eslint:recommended", + "parserOptions": { + "sourceType": "module" + }, + "rules": { + "no-console": "off", + "indent": [ "error", 2 ], + "quotes": ["error", "single", { "allowTemplateLiterals": true }], + "comma-dangle": ["error", "always-multiline"], + "semi": [ "error", "always" ] + } +} diff --git a/lab-katherine/.gitignore b/lab-katherine/.gitignore new file mode 100644 index 0000000..9497282 --- /dev/null +++ b/lab-katherine/.gitignore @@ -0,0 +1,145 @@ +# 401 JS +db +.env +temp +build + +# Created by https://www.gitignore.io/api/vim,osx,node,linux,windows +### 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* + +### 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 ### +*.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 + +### 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/vim,osx,node,linux,windows diff --git a/README.md b/lab-katherine/README.md similarity index 100% rename from README.md rename to lab-katherine/README.md diff --git a/lab-katherine/__test__/server.test.js b/lab-katherine/__test__/server.test.js new file mode 100644 index 0000000..5928e0b --- /dev/null +++ b/lab-katherine/__test__/server.test.js @@ -0,0 +1,28 @@ +'use strict'; + +const superagent = require('superagent'); + +describe('POST /api/cowsay', () => { + test('should respond with 200 response and echo the body', () => { + return superagent.post('http://localhost:4000/api/cowsay') + .send({ + title: 'hello world', + }) + .then(res => { + expect(res.status).toEqual(200); + expect(res.body).toEqual({title: 'hello world'}); + }); + }); + + test('should respond with a 400', () => { + return superagent.post('http://localhost:3000/api/cowsay') + .set({ 'Content-Type': 'application/json'}) + .send('{') + .then(Promise.reject) + .catch(res => { + expect(res.status).toEqual(400); + console.log(res); + expect(res.response.text).toEqual('bad request'); + }); + }); +}); diff --git a/lab-katherine/index.js b/lab-katherine/index.js new file mode 100644 index 0000000..d4e3d79 --- /dev/null +++ b/lab-katherine/index.js @@ -0,0 +1,10 @@ +'use strict'; + +// load environment +require('dotenv').config(); + +// load dependencies +const server = require('./lib/server.js'); + +// start server +server.start(process.env.PORT, () => console.log('server up ::', process.env.PORT)); diff --git a/lab-katherine/lib/request-parser.js b/lab-katherine/lib/request-parser.js new file mode 100644 index 0000000..153eb03 --- /dev/null +++ b/lab-katherine/lib/request-parser.js @@ -0,0 +1,31 @@ +'use strict'; + +const url = require('url'); +const queryString = require('querystring'); + +// The request parser module should return a promise +// that parses the request url, querystring, +// and POST or PUT body (as JSON). +module.exports = (req) => { + return new Promise((resolve, reject) => { + req.url = url.parse(req.url); + req.url.query = queryString.parse(req.url.query); + + if(!(req.method === 'POST' || req.method === 'PUT')) + return resolve(req); + + let text = ''; + + req.on('data', (buffer) => { + text += buffer.toString(); + }); + req.on('end', () => { + try { + req.body = JSON.parse(text); + resolve(req); + } catch (err) { + reject(err); + } + }); + }); +}; diff --git a/lab-katherine/lib/server.js b/lab-katherine/lib/server.js new file mode 100644 index 0000000..b5b4ea4 --- /dev/null +++ b/lab-katherine/lib/server.js @@ -0,0 +1,109 @@ +'use strict'; + +// HTTP SERVER defining all route behavior and exporting an interface +// for starting and stopping the server +// It should export an object with start and stop methods + +// node dependencies +const http = require('http'); +const requestParser = require('./request-parser.js'); +const cowsay = require('cowsay'); + +// functionality +const app = http.createServer((req, res) => { + + requestParser(req) + .then(req => { + // route-handling + + // When a client makes a GET request to / + // the server should send back html with a project description + // and an anchor to /cowsay. + if(req.method === 'GET' && req.url.pathname === '/'){ + res.writeHead(200, {'Content-Type': 'text/html'}); + res.write(` + +
+
+ ${cowsay.say({text: output})}
+
+
+ `);
+ res.end();
+ return;
+ }
+
+ // When a client makes a POST request to /api/cowsay
+ // it should send JSON that includes {"text": "