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
21 changes: 21 additions & 0 deletions lab-shawn/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"rules": {
"no-console": "off",
"indent": [ "error", 2 ],
"quotes": [ "error", "single" ],
"semi": ["error", "always"],
"linebreak-style": [ "error", "unix" ]
},
"env": {
"es6": true,
"node": true,
"mocha": true,
"jasmine": true
},
"ecmaFeatures": {
"modules": true,
"experimentalObjectRestSpread": true,
"impliedStrict": true
},
"extends": "eslint:recommended"
}
127 changes: 127 additions & 0 deletions lab-shawn/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
Created by https://www.gitignore.io/api/macos,node,vim,windows,linux

out.bmp
### macOS ###
*.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


### Node ###
node_modules
# Logs
logs
*.log
npm-debug.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

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# 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



### Vim ###
# swap
[._]*.s[a-w][a-z]
[._]s[a-w][a-z]
# session
Session.vim
# temporary
.netrwhist
*~
# auto-generated tag files
tags


### Windows ###
# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk


### 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*
23 changes: 23 additions & 0 deletions lab-shawn/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# **Vannilla HTTP server**

## **Overview**

This application is a vanilla http api that makes GET and POST requests to the server.


## **How To Use API**
* Clone this repository
* Open a terminal and run `npm i` to install all the application dependencies

### **Run your server**
`node server.js`

In a new terminal window/tab run your HTTP method commands

### **GET Request**
`http GET localhost:[port number]/`

`http GET localhost:[port number]/cowsay?text='input text' `

### **POST Request**
`http POST localhost:[port number]/cowsay text='input text'`
13 changes: 13 additions & 0 deletions lab-shawn/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const gulp = require('gulp');
const eslint = require('gulp-eslint');

gulp.task('lint', function(){
return gulp.src(['**/*.js', '!node_modules'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});

gulp.task('default',['lint']);
16 changes: 16 additions & 0 deletions lab-shawn/lib/body-parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

module.exports = function(req,callback){
req.body = '';
req.on('data',function(data){
req.body += data.toString();
});
req.on('end', function(){
try{
req.body = JSON.parse(req.body);
callback(null,req.body);
}catch(err){
callback(err);
}
});
};
21 changes: 21 additions & 0 deletions lab-shawn/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "lab-shawn",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cowsay": "^1.1.9"
},
"devDependencies": {
"gulp": "^3.9.1",
"gulp-eslint": "^3.0.1",
"gulp-mocha": "^3.0.1"
}
}
46 changes: 46 additions & 0 deletions lab-shawn/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

const http = require('http');
const url = require('url');
const querystring = require('querystring');
const cowsay = require('cowsay');
const parseBody = require('./lib/body-parser.js');
const PORT = process.env.port || 3000;

const server = http.createServer(function(req,res){
req.url = url.parse(req.url);
req.url.query = querystring.parse(req.url.query);

if(req.method === 'GET' && req.url.pathname === '/'){
res.writeHead(200,{'Content-Type': 'text/plain'});
res.write(cowsay.say({text: 'Hello from my server'}))
res.end();
}

if(req.method === 'GET' && req.url.pathname === '/cowsay'){
if(!req.url.query.text){
res.writeHead(400, {'Content-Type': 'text/plain'});
res.write(cowsay.say({f:'dragon',text: 'bad request'}));
res.end();
}else{
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(cowsay.say({f:'dragon', text: req.url.query.text}));
res.end();
}
}
if(req.method === 'POST' && req.url.pathname === '/cowsay' ){
parseBody(req, function(err){
if(err) console.error(err);
res.writeHead(200, {
'Content-Type':'text/plain'
});
res.write(cowsay.say({f: 'dragon', text: req.body.text}));
res.end();
});
}

});

server.listen(PORT, () => {
console.log(`Port: ${PORT} served up`);
});