diff --git a/lab-mark/.eslintignore b/lab-mark/.eslintignore new file mode 100644 index 0000000..05b1cf3 --- /dev/null +++ b/lab-mark/.eslintignore @@ -0,0 +1,5 @@ +**/node_modules/* +**/vendor/* +**/*.min.js +**/coverage/* +**/build/* diff --git a/lab-mark/.eslintrc.json b/lab-mark/.eslintrc.json new file mode 100644 index 0000000..840d336 --- /dev/null +++ b/lab-mark/.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-mark/.gitignore b/lab-mark/.gitignore new file mode 100644 index 0000000..3184cd5 --- /dev/null +++ b/lab-mark/.gitignore @@ -0,0 +1,147 @@ +# 401 JS +package-lock.json +../package-lock.json +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/lab-mark/README.md b/lab-mark/README.md new file mode 100644 index 0000000..47c5582 --- /dev/null +++ b/lab-mark/README.md @@ -0,0 +1,14 @@ +![CF](https://camo.githubusercontent.com/70edab54bba80edb7493cad3135e9606781cbb6b/687474703a2f2f692e696d6775722e636f6d2f377635415363382e706e67) Code-401-Javascript lab-7: HTTP-Server +=== +This is the day 7 lab with Code Fellows. The purpose of the lab is to teach students to use vanilla HTTP requests to create a server. + +# Modules +Description of exported values of each module defined in lib/ directory, along with airity and expected input/return values. +### Server.js +Houses the code for the server. It exports an object with start and stop methods which either starts or stops the server. Currently only start is being invoked in index.js. +* #### start + * Has an arity of two, which takes in a port number and callback function. Port number is preset to 3000 and the callback is a success function which logs to the server which port the server is running on. +* #### stop + * Has an arity of one, which takes in a callback function. Currently not being utilized. +### request-parser.js +Exports a function with an arity of one, that takes in a request and returns a new promise. It parses the data depending if its a GET or POST request. diff --git a/lab-mark/__test__/server.test.js b/lab-mark/__test__/server.test.js new file mode 100644 index 0000000..366985a --- /dev/null +++ b/lab-mark/__test__/server.test.js @@ -0,0 +1,63 @@ +'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:3000/api/cowsay') + .send({ + text: 'Wassup', + }) + .then(res => { + expect(res.status).toEqual(200); + expect(res.body).toEqual({'content': ` ________ +< Wassup > + -------- + \\ ^__^ + \\ (oo)\\_______ + (__)\\ )\\/\\ + ||----w | + || ||`}); + }); + }); + + test('should respond with a 400', () => { + return superagent.post('http://localhost:3000/api/cosway') + .set({ 'Content-Type': 'application/json'}) + .send('{') + .then(Promise.reject) + .catch(res => { + expect(res.status).toEqual(400); + expect(res.response.text).toEqual('bad request'); + }); + }); +}); + +describe('GET /api/cowsay', () => { + test('should respond with 200 response and echo the body', () => { + return superagent.get('http://localhost:3000/api/cowsay') + .query('text=lulwat') + .then(res => { + expect(res.status).toEqual(200); + expect(res.body).toEqual({'content': ` ________ +< lulwat > + -------- + \\ ^__^ + \\ (oo)\\_______ + (__)\\ )\\/\\ + ||----w | + || ||`}); + }); + }); + + test('should respond with a 400', () => { + return superagent.get('http://localhost:3000/api/cosway') + .set({ 'Content-Type': 'application/json'}) + .query('txt=lulwat') + .then(Promise.reject) + .catch(res => { + expect(res.status).toEqual(400); + expect(res.response.text).toEqual('bad request'); + }); + }); +}); diff --git a/lab-mark/index.js b/lab-mark/index.js new file mode 100644 index 0000000..5e60742 --- /dev/null +++ b/lab-mark/index.js @@ -0,0 +1,9 @@ +'use strict'; + +// load environment +require('dotenv').config(); +const server = require('./lib/server.js'); + +// start server +server.start(process.env.PORT, () => + console.log('server up ::', process.env.PORT)); diff --git a/lab-mark/lib/request-parser.js b/lab-mark/lib/request-parser.js new file mode 100644 index 0000000..ad74b8b --- /dev/null +++ b/lab-mark/lib/request-parser.js @@ -0,0 +1,32 @@ +'use strict'; + +const url = require('url'); +const queryString = require('querystring'); +const cowsay = require('cowsay'); + +module.exports = (req) => { + return new Promise((resolve, reject) => { + req.url = url.parse(req.url); + req.url.query = queryString.parse(req.url.query); + console.log(req.url.query); + // Resolve if GET + if(!(req.method === 'POST' || req.method === 'PUT')) + return resolve(req); + + req.body = {}; + let content = ''; + + req.on('data', (buffer) => { + content += buffer.toString(); + }); + req.on('end', () => { + try { + console.log(content); + req.body.content = cowsay.say(JSON.parse(content)); + resolve(req); + } catch (err) { + reject(err); + } + }); + }); +}; diff --git a/lab-mark/lib/server.js b/lab-mark/lib/server.js new file mode 100644 index 0000000..2b45343 --- /dev/null +++ b/lab-mark/lib/server.js @@ -0,0 +1,100 @@ +'use strict'; + +const http = require('http'); +const requestParser = require('./request-parser.js'); +const cowsay = require('cowsay'); + +const app = http.createServer((req, res) => { + + requestParser(req) + .then(req => { + // handle rotues + if(req.method === 'GET' && req.url.pathname === '/'){ + res.writeHead(200, {'Content-Type': 'text/html'}); + res.write(` + + + + cowsay + + +
+ +
+
+ Click on the cowsay anchor tag to get the default cow and cow text.
+ To change the text, add '?text=message to the end of the url.
+ To change the cow, add &f=dragon after the message.' +
+ + + `); + res.end(); + return; // break out of the (req, res) => {} callback + } + + if(req.method === 'GET' && req.url.pathname === '/cowsay'){ + let cowMessage = { text: 'I need something good to say!' }; + if (req.url.query.text) + cowMessage = req.url.query; + + res.writeHead(200, {'Content-Type': 'text/html'}); + res.write(` + + + + cowsay + + +

cowsay

+
${cowsay.say(cowMessage)}
+ + + `); + res.end(); + return; // break out of the (req, res) => {} callback + } + + if (req.method === 'GET' && req.url.pathname === '/api/cowsay') { + let cowMessage = { text: 'I need something good to say!' }; + if (req.url.query.text) + cowMessage = req.url.query; + + res.writeHead(200, {'Content-Type': 'application/json'}); + res.write(JSON.stringify({content: cowsay.say(cowMessage)})); + res.end(); + return; + } + + if(req.method === 'POST' && req.url.pathname === '/api/cowsay'){ + + res.writeHead(200, {'Content-Type': 'application/json'}); + res.write(JSON.stringify(req.body)); + res.end(); + return; // break out of the (req, res) => {} callback + } + + // 404 for any request to a non route + // respond to the client + res.writeHead(404, { + 'Content-Type': 'text/plain', + }); + res.write(`resource ${req.url.pathname} not found!`); + res.end(); + }) + .catch(err => { + console.log(err); + res.writeHead(400, { 'Content-Type': 'text/plain' }); + res.write('bad request'); + res.end(); + }); +}); + +module.exports = { + start: (port, callback) => app.listen(port, callback), + stop: (callback) => app.close(callback), +}; diff --git a/lab-mark/package.json b/lab-mark/package.json new file mode 100644 index 0000000..ba22a0d --- /dev/null +++ b/lab-mark/package.json @@ -0,0 +1,29 @@ +{ + "name": "lab-mark", + "version": "1.0.0", + "description": "", + "main": "index.js", + "directories": { + "lib": "lib" + }, + "scripts": { + "start": "node index.js", + "watch": "nodemon index.js", + "lint": "eslint .", + "test": "jest --coverage -i", + "test-watch": "jest --watch -i" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "dotenv": "^4.0.0", + "jest": "^21.1.0", + "nodemon": "^1.12.1" + }, + "dependencies": { + "cowsay": "^1.2.1", + "faker": "^4.1.0", + "superagent": "^3.6.0" + } +}