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
Binary file added .DS_Store
Binary file not shown.
21 changes: 21 additions & 0 deletions lab-jinho/.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"
}
146 changes: 146 additions & 0 deletions lab-jinho/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
Skip to content
This repository
Search
Pull requests
Issues
Gist
@jmpaik
Watch 5
Star 1
Fork 8 codefellows/seattle-javascript-401d12
Code Issues 0 Pull requests 0 Projects 0 Wiki Pulse Graphs
Branch: master Find file Copy pathseattle-javascript-401d12/06-tcp_servers/demo/chat-server/.gitignore
78ba337 a day ago
@bnates bnates added lecture 06
1 contributor
RawBlameHistory
128 lines (94 sloc) 1.85 KB
# Created by https://www.gitignore.io/api/node,vim,macos,linux,windows

node_modules/

### Node ###
# 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


### 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


### 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*


### 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
Contact GitHub API Training Shop Blog About
© 2016 GitHub, Inc. Terms Privacy Security Status Help
28 changes: 28 additions & 0 deletions lab-jinho/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Cowsay API
=============

>

## Getting Started


### Prereqs

- dependencies:
cowsay: ^1.1.9 --> npm install -S cowsay


## Running

In root server, type in the command **"node server.js"** in first terminal pane.

In Second Terminal Pane :

- For Get Request:
``` http localhost:3000/ : will respond with Hello From The Server```
``` http localhost:3000/cowsay text=='some text' : will respond with cowsay```

In Third Terminal Pane:
- For POST Request:
``` we need a json file in root directory like: data.json with simple json string```
``` cat data.json | http post localhost:3000/cowsay : will show json data using cowsay```
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job adding docs! Get in the practice of making them thorough as they will need to be more detailed for future assignments.

23 changes: 23 additions & 0 deletions lab-jinho/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

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

gulp.task('test', function(){
gulp.src('./test/*-test.js', {read: false})
.pipe(mocha({reporter: 'spec'}));
});

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

gulp.task('dev', function(){
gulp.watch(['**/*.js','!node_modules/**'], ['lint', 'test']);
});

gulp.task('default', ['dev']);
18 changes: 18 additions & 0 deletions lab-jinho/lib/parse-body.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'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);
}
});
};
16 changes: 16 additions & 0 deletions lab-jinho/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "lab-jinho",
"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"
}
}
62 changes: 62 additions & 0 deletions lab-jinho/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict';

//**DEPENDENCIES**
//node modules
const http = require('http');
const url = require('url');
const queryString = require('querystring');
//npm modules
const cowsay = require('cowsay');
//custom modules
const parseBody = require('./lib/parse-body.js');
//environment variables
const PORT = process.env.PORT || 3000;

//**LOGIC**
const server = http.createServer(function(req, res) {
req.url = url.parse(req.url);
req.url.query = queryString.parse(req.url.query);

//Logic: Method: GET
if (req.method === 'GET') {
if(req.url.pathname === '/') {
res.writeHead( 200, {'Content-Type': 'text/plain'});
res.write('Hello From The Server');
res.end();
}
if (req.url.pathname === '/cowsay'){
if (req.url.query.text) {
res.writeHead(200);
res.write(cowsay.say({text:req.url.query.text}));
res.end();
return;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'bad request' isn't being displayed in the cow, you could add wrap the following logic in an if that handles the 400 if req.url.query.text does not exist.

res.writeHead(400);
res.write(cowsay.say({text:'bad request'}));
res.end();
}
}

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

//**START SERVER**
server.listen(PORT, function() {
console.log('server up:', PORT);
});