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-katherine/.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-katherine/.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" ]
}
}
145 changes: 145 additions & 0 deletions lab-katherine/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# 401 JS
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
File renamed without changes.
28 changes: 28 additions & 0 deletions lab-katherine/__test__/server.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'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:4000/api/cowsay')
.send({
title: 'hello world',
})
.then(res => {
expect(res.status).toEqual(200);
expect(res.body).toEqual({title: 'hello world'});
});
});

test('should respond with a 400', () => {
return superagent.post('http://localhost:3000/api/cowsay')
.set({ 'Content-Type': 'application/json'})
.send('{')
.then(Promise.reject)
.catch(res => {
expect(res.status).toEqual(400);
console.log(res);
expect(res.response.text).toEqual('bad request');
});
});
});
10 changes: 10 additions & 0 deletions lab-katherine/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

// load environment
require('dotenv').config();

// load dependencies
const server = require('./lib/server.js');

// start server
server.start(process.env.PORT, () => console.log('server up ::', process.env.PORT));
31 changes: 31 additions & 0 deletions lab-katherine/lib/request-parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

const url = require('url');
const queryString = require('querystring');

// The request parser module should return a promise
// that parses the request url, querystring,
// and POST or PUT body (as JSON).
module.exports = (req) => {
return new Promise((resolve, reject) => {
req.url = url.parse(req.url);
req.url.query = queryString.parse(req.url.query);

if(!(req.method === 'POST' || req.method === 'PUT'))
return resolve(req);

let text = '';

req.on('data', (buffer) => {
text += buffer.toString();
});
req.on('end', () => {
try {
req.body = JSON.parse(text);
resolve(req);
} catch (err) {
reject(err);
}
});
});
};
109 changes: 109 additions & 0 deletions lab-katherine/lib/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
'use strict';

// HTTP SERVER defining all route behavior and exporting an interface
// for starting and stopping the server
// It should export an object with start and stop methods

// node dependencies
const http = require('http');
const requestParser = require('./request-parser.js');
const cowsay = require('cowsay');

// functionality
const app = http.createServer((req, res) => {

requestParser(req)
.then(req => {
// route-handling

// When a client makes a GET request to /
// the server should send back html with a project description
// and an anchor to /cowsay.
if(req.method === 'GET' && req.url.pathname === '/'){
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(`<!DOCTYPE html>
<html>
<head>
<title> cowsay </title>
</head>
<body>
<header>
<nav>
<ul>
<li><a href="/cowsay">cowsay</a></li>
</ul>
</nav>
<header>
<main>
<ul>
<li>Click the cowsay link to get the cow's default message.</li>
<li>If you want the cow to say anything else, add "?text="
followed by your message to the end of the URL and refresh.</li>
</ul>
</main>
</body>
</html>`);
res.end();
return;
}

// When a client makes a GET request to /cowsay?text={message}
// the server should parse the querystring for a text key.
// It should then send a rendered HTML page with a cowsay cow
// speaking the value of the text query.
// If there is no text query, the cow message should say
// 'I need something good to say!'.
if(req.method === 'GET' && req.url.pathname === '/cowsay'){
let output;
if (req.url.query.text)
output = req.url.query.text;
else
output = 'I need something good to say!';

res.writeHead(200, {'Content-Type': 'text/html'});
res.write(`<!DOCTYPE html>
<html>
<head>
<title> cowsay </title>
</head>
<body>
<h1> cowsay </h1>
<pre>
${cowsay.say({text: output})}
</pre>
</body>
</html>`);
res.end();
return;
}

// When a client makes a POST request to /api/cowsay
// it should send JSON that includes {"text": "<message>"}.
// The server should respond with a JSON body {"content": "<cowsay cow>"}.
if(req.method === 'POST' && req.url.pathname === '/api/cowsay'){
res.writeHead(200, {'Content-Type': 'application/json'});
res.write(JSON.stringify(req.body));
res.end();
return;
}

// for a non-route
res.writeHead(404, {
'Content-Type': 'text/plain',
});
res.write(`resource ${req.url.pathname} not found!`);
res.end();
})
.catch(err => {
console.log(err);
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.write('bad request');
res.end();
});
});

// export interface
module.exports = {
start: (port, callback) => app.listen(port, callback),
stop: (callback) => app.close(callback),
};
Loading