Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lab-christina/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**/node_modules/*
**/vendor/*
**/*.min.js
**/coverage/*
**/build/*
26 changes: 26 additions & 0 deletions lab-christina/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -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" ]
}
}
128 changes: 128 additions & 0 deletions lab-christina/.gitignore
Original file line number Diff line number Diff line change
@@ -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
Empty file added lab-christina/README.md
Empty file.
19 changes: 19 additions & 0 deletions lab-christina/__test__/server.test.js
Original file line number Diff line number Diff line change
@@ -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', () => {

});
});
9 changes: 9 additions & 0 deletions lab-christina/index.js
Original file line number Diff line number Diff line change
@@ -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);
});
29 changes: 29 additions & 0 deletions lab-christina/lib/request-parser.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
});
};
60 changes: 60 additions & 0 deletions lab-christina/lib/server.js
Original file line number Diff line number Diff line change
@@ -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(`<!DOCTYPE html>
<html>
<head><title></title></head>
<body>
<header>
<nav><ul><li><a href ='https://www.npmjs.com/package/cowsay'>cowsay</a></li></ul></nav>
</header>
<main>
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.
</main>
</body>
</html>`);
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),
};
23 changes: 23 additions & 0 deletions lab-christina/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}