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-mark/.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-mark/.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" ]
}
}
147 changes: 147 additions & 0 deletions lab-mark/.gitignore
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions lab-mark/README.md
Original file line number Diff line number Diff line change
@@ -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.
63 changes: 63 additions & 0 deletions lab-mark/__test__/server.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
9 changes: 9 additions & 0 deletions lab-mark/index.js
Original file line number Diff line number Diff line change
@@ -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));
32 changes: 32 additions & 0 deletions lab-mark/lib/request-parser.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
});
};
Loading