diff --git a/lab-christina/.eslintignore b/lab-christina/.eslintignore new file mode 100644 index 0000000..05b1cf3 --- /dev/null +++ b/lab-christina/.eslintignore @@ -0,0 +1,5 @@ +**/node_modules/* +**/vendor/* +**/*.min.js +**/coverage/* +**/build/* diff --git a/lab-christina/.eslintrc.json b/lab-christina/.eslintrc.json new file mode 100644 index 0000000..840d336 --- /dev/null +++ b/lab-christina/.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-christina/.gitignore b/lab-christina/.gitignore new file mode 100644 index 0000000..8922811 --- /dev/null +++ b/lab-christina/.gitignore @@ -0,0 +1,128 @@ + +# Created by https://www.gitignore.io/api/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 + +### 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,node,linux,windows diff --git a/lab-christina/README.md b/lab-christina/README.md new file mode 100644 index 0000000..e69de29 diff --git a/lab-christina/__test__/server.test.js b/lab-christina/__test__/server.test.js new file mode 100644 index 0000000..f8089ae --- /dev/null +++ b/lab-christina/__test__/server.test.js @@ -0,0 +1,19 @@ +'use strict'; + +const superagent = require('superagent'); +const server = require('../lib/server.js'); + +describe('POST', () => { + test('Should respond with a status code of 200', () => { + expect(response.status).toEqual(200); + expect(response.body).toEqual(response.body) + }); + + test('Should respond with a status code of 400', () => { + + }); + + test('Should respond with a status code of 404', () => { + + }); +}); diff --git a/lab-christina/index.js b/lab-christina/index.js new file mode 100644 index 0000000..a6a6a94 --- /dev/null +++ b/lab-christina/index.js @@ -0,0 +1,9 @@ +'use strict'; + +const dotenv = require('dotenv').config(); +const server = require('./lib/server.js'); +const PORT = 3000; + +server.start(PORT, () => { + console.log('server ::', PORT); +}); diff --git a/lab-christina/lib/request-parser.js b/lab-christina/lib/request-parser.js new file mode 100644 index 0000000..8ef1764 --- /dev/null +++ b/lab-christina/lib/request-parser.js @@ -0,0 +1,29 @@ +'use strict'; + +const url = require('url'); +const queryString = require('queryString');//lecture code + +module.exports = (request) => { + return new Promise((resolve, reject) => { + request.url = url.parse(request.url); + request.url.query = queryString.parse(request.url.query); + + if(!(request.method === 'POST' || request.method === 'PUT')) + return resolve(request); + + let text = ''; + // only parse the POST || PUT request bodies + request.on('data', (buffer) => { + text += buffer.toString(); + }); + + request.on('end', () => { + try { + request.body = JSON.parse(text);//try catch is syntax thing. .catch() is a method on a promise + resolve(request);//parsing because express does it this way + } catch (err) { + reject(err); + } + }); + }); +}; diff --git a/lab-christina/lib/server.js b/lab-christina/lib/server.js new file mode 100644 index 0000000..c1981ff --- /dev/null +++ b/lab-christina/lib/server.js @@ -0,0 +1,60 @@ +//node dependencies +const http = require('http'); +const requestParser = require('./request-parser.js'); +//npm dependencies +//constants +let cowsay = require('cowsay'); +const app = http.createServer((request, response) => { + + requestParser(request) + .then(request => { + if(request.method === 'GET' && request.url.pathname === '/'){ + response.writeHead(200, {'Content-Type': 'text/html'}); + response.write(` + + + +
+ +
+
+ Using cowsay API to creat GET/POST/PUT request through and http server. + Responses are handled, parsed and tested. All I want to do is style this page. +
+ + `); + response.end(); + return; + } + + if(request.method === 'GET' && request.url.pathname === '/cowsay?text={message}'){ + response.writeHead(200, {'Content-Type': 'JSON'}); + response.write(); + response.write(cowsay.think({ + text: 'I\'m a bird', + e: '@@', + T: 'U', + wrap: false, + })); + response.end(); + return; + } + + + response.writeHead(404, {'Content-Type': 'text/plain'}); + response.write('__ERROR__400__path not found'); + response.end(); + + }) + .catch(error => { + console.log(error); + response.writeHead(400, {'Content-Type': 'text/plain'}); + response.write('__ERR0R__404__bad request'); + response.end(); + }); +}); + +module.exports = { + start: (PORT, callback) => app.listen(PORT, callback), + stop: (callback) => app.close(callback), +}; diff --git a/lab-christina/package.json b/lab-christina/package.json new file mode 100644 index 0000000..14a78b6 --- /dev/null +++ b/lab-christina/package.json @@ -0,0 +1,23 @@ +{ + "name": "lab-christina", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "start": "node index.js", + "watch": "nodemon index.js", + "test": "jest -i --coverage" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "dotenv": "^4.0.0", + "jest": "^21.1.0", + "querystring": "^0.2.0", + "superagent": "^3.6.0" + }, + "devDependencies": { + "nodemon": "^1.12.1" + } +}