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
6 changes: 6 additions & 0 deletions lab-nathan/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
**/node_modules/*
**/vendor/*
**/*.min.js
**/coverage/*
**/build/*
**/assets/*
23 changes: 23 additions & 0 deletions lab-nathan/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"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
},
"parserOptions": {
"ecmaFeatures": {
"modules": true,
"experimentalObjectRestSpread": true,
"impliedStrict": true
}
},
"extends": "eslint:recommended"
}
138 changes: 138 additions & 0 deletions lab-nathan/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# Created by https://www.gitignore.io/api/osx,vim,node,macos,windows

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

# Icon must end with two \r

# Thumbnails

# Files that might appear in the root of a volume

# Directories potentially created on remote AFP share

### 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/osx,vim,node,macos,windows

output
78 changes: 78 additions & 0 deletions lab-nathan/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Nathan's HTTP Server

This is a simple HTTP server which can display a cow saying a custom message. There are two ways to communicate with the server, a GET request or a POST request.

## GET
```
/help

/[cowsay?text='<message>'[&cow='<cow>']]
```

## POST

URL
```
/cowsay
```

Body
```js
{
"text": "<message>",
"cow": "<cow>"
}

```

## Cows
* beavis.zen
* bong
* bud-frogs
* bunny
* cheese
* cower
* daemon
* default
* doge
* dragon-and-cow
* dragon
* elephant-in-snake
* elephant
* eyes
* flaming-sheep
* ghostbusters
* goat
* head-in
* hedgehog
* hellokitty
* kiss
* kitty
* koala
* kosh
* luke-koala
* mech-and-cow
* meow
* milk
* moofasa
* moose
* mutilated
* ren
* satanic
* sheep
* skeleton
* small
* sodomized
* squirrel
* stegosaurus
* stimpy
* supermilker
* surgery
* telebears
* turkey
* turtle
* tux
* vader-koala
* vader
* whale
* www
20 changes: 20 additions & 0 deletions lab-nathan/lib/parse-body.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

module.exports = parseBody;

function parseBody(request, callback) {
request.body = '';

request.on('data', function(data) {
request.body += data.toString();
});

request.on('end', function() {
try {
request.body = JSON.parse(request.body);
callback(null, request.body);
} catch (err) {
callback(err);
}
});
}
16 changes: 16 additions & 0 deletions lab-nathan/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "lab-nathan",
"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"
}
}
109 changes: 109 additions & 0 deletions lab-nathan/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
'use strict';

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

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

if (request.url.pathname === '/') {
response.writeHead(200, { 'content-type': 'text/plain' });
response.write('Welcome to Nathan\'s HTTP Server!');
response.write(os.EOL);
response.write('GET /help for a list of commands.');
response.write(os.EOL);
response.end();
return;
}

switch (request.method) {
case 'GET':
processGetRequest(request, response);
break;
case 'POST':
processPostRequest(request, response);
break;
default:
processDefaultRequest(request, response);
break;
}
});

function processGetRequest(request, response) {
if (request.url.pathname === '/help') {
response.writeHead(200, { 'content-type': 'text/plain' });
response.write('Query Parameters:');
response.write(os.EOL);
response.write('"text": What the cow will say.');
response.write(os.EOL);
response.write('"cow": Which cow to use.');
response.write(os.EOL);
response.write(os.EOL);
response.write('Cows:');
response.write(os.EOL);
cowsay.list(function(err, files) {
files.forEach(function(file) {
response.write(file);
response.write(os.EOL);
});

response.end();
});
}
else if (request.url.query.text) {
let cowOptions = {};
cowOptions.text = request.url.query.text;

if (request.url.query.cow) {
cowOptions.f = request.url.query.cow;
}

response.writeHead(200, { 'content-type': 'text/plain' });
response.write(cowsay.say(cowOptions));
response.end();
}
else {
writeBadRequest(request, response);
response.end();
}
}

function processPostRequest(request, response) {
parseBody(request, function(err) {
if (err || !request.body.text) {
writeBadRequest(request, response);
} else {
let cowOptions = {};
cowOptions.text = request.body.text;

if (request.body.cow) {
cowOptions.f = request.body.cow;
}
response.writeHead(200, { 'content-type': 'text/plain' });
response.write(cowsay.say(cowOptions));
}

response.end();
});
}

function writeBadRequest(request, response) {
response.writeHead(400, { 'content-type': 'text/plain' });
response.write(cowsay.say({text: 'bad request'}));
}

function processDefaultRequest(request, response) {
response.writeHead(400, { 'content-type': 'text/plain' });
response.write('Only GET and POST requests are currently supported.');
response.end();
}

server.listen(PORT, function() {
console.log('Listening on port:', PORT);
});