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 .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"
}
136 changes: 136 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# 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
91 changes: 38 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,38 @@
![cf](https://i.imgur.com/7v5ASc8.png) 11: Single Resource Express API
======

## Submission Instructions
* fork this repository & create a new branch for your work
* write all of your code in a directory named `lab-` + `<your name>` **e.g.** `lab-susan`
* push to your repository
* submit a pull request to this repository
* submit a link to your PR in canvas
* write a question and observation on canvas

## Learning Objectives
* students will be able to create a single resource API using the express framework
* students will be able to leverage 3rd party helper modules for debugging, logging, and handling errors

## Requirements

#### Configuration
* `package.json`
* `.eslintrc`
* `.gitignore`
* `README.md`
* your `README.md` should include detailed instructions on how to use your API

#### Feature Tasks
* create an HTTP server using `express`
* create a object constructor that creates a _simple resource_ with at least 3 properties
* it can **not** have the same properties as the in-class sample code (other than the `id`)
* a unique `id` property should be included *(node-uuid)*
* include two additional properties of your choice
* use the JSON parser included with the `body-parser` module as a middleware component to parse the request body on `POST` and `PUT` routes
* use the npm `debug` module to log the methods in your application
* create an `npm` script to automate the `debug` process and start the server
* persist your API data using the storage module and file system persistence

#### Server Endpoints
* **`/api/simple-resource-name`**
* `POST` request
* pass data as stringifed JSON in the body of a **POST** request to create a new resource
* `GET` request
* pass `?id=<uuid>` as a query string parameter to retrieve a specific resource (as JSON)
* `DELETE` request
* pass `?id=<uuid>` in the query string to **DELETE** a specific resource
* this should return a 204 status code with no content in the body

#### Tests
* write a test to ensure that your api returns a status code of 404 for routes that have not been registered
* write tests to ensure the `/api/simple-resource-name` endpoint responds as described for each condition below:
* `GET`: test 404, it should respond with 'not found' for valid requests made with an id that was not found
* `GET`: test 400, it should respond with 'bad request' if no id was provided in the request
* `GET`: test 200, it should contain a response body for a request made with a valid id
* `POST`: test 400, it should respond with 'bad request' if no request body was provided or the body was invalid
* `POST`: test 200, it should respond with the body content for a post request with a valid body
#Overview

* This RESTful API provides the back-end infrastructure and funtionality to create, read, update, and delete data relative to superheroes.

#Architecture

<p>This API is structured on a Model View Controller(MVC) architectue pattern. The basic technologies are: node.js server, node.http module, our own "express like" middleware.</p>

<p>Middleware:</p>

* We wrote a vanilla version of our own router that handles the base routing.

* We wrote a vanilla version of a body-parser module that parses JSON data.

#API endpoints

##POST /api/superhero

<p>Required Data:</p>

* Provide superhero name, the comic universe where they are from as JSON.

<p>This route will create a new superhero by providing a superhero name and a comic book universe from which they reside in the body of the request.</p>

##GET /api/superhero

<p>Required Data:</p>

* Provide a specific unique superhero id.

<p>This route will require a unique superhero id and grab the specific JSON object associated with that id.</p>

#Testing

* Testing Framework mocha test runner
* chai(expect)
* bluebird promise library
* eslint
1 change: 1 addition & 0 deletions data/superhero/04d9f4f9-3eda-4a4d-9d12-7e53e1296030.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"04d9f4f9-3eda-4a4d-9d12-7e53e1296030","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/0c63b92a-4e9e-460b-869e-60a6db4f4376.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"0c63b92a-4e9e-460b-869e-60a6db4f4376","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/0ff87c8a-1b91-4d6c-8cd9-9519bf8ed854.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"0ff87c8a-1b91-4d6c-8cd9-9519bf8ed854","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/29f74f26-995f-41ee-876d-185d12c6a0a5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"29f74f26-995f-41ee-876d-185d12c6a0a5","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/2fad148c-6270-4f9c-91c3-98298816516e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"2fad148c-6270-4f9c-91c3-98298816516e","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/30ae2331-481d-48c5-a969-ceece81f92b6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"30ae2331-481d-48c5-a969-ceece81f92b6","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/3997b53b-30e4-415e-b5e5-411a80890b57.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"3997b53b-30e4-415e-b5e5-411a80890b57","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/41053efe-70bb-4b45-91a1-2568dcd7c275.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"41053efe-70bb-4b45-91a1-2568dcd7c275","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/64e8a01b-c71a-4a61-9ad1-2e814f65f48d.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"64e8a01b-c71a-4a61-9ad1-2e814f65f48d","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/650f9531-8614-4d95-9a7f-7aed5488f060.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"650f9531-8614-4d95-9a7f-7aed5488f060","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/71cb0b3d-dd06-4785-a143-9d749d740184.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"71cb0b3d-dd06-4785-a143-9d749d740184","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/772d3f92-9b38-4b2c-8f77-8d35eea7a7ab.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"772d3f92-9b38-4b2c-8f77-8d35eea7a7ab","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/7d2ce11c-d782-4b07-893e-e53d4b5b1e11.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"7d2ce11c-d782-4b07-893e-e53d4b5b1e11","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/83f9c5ff-414f-43fc-bd72-0933461a78cf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"83f9c5ff-414f-43fc-bd72-0933461a78cf","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/8989cf0e-7039-45c6-818f-9ded0e6128d0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"8989cf0e-7039-45c6-818f-9ded0e6128d0","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/9816731e-a4e5-4802-97f6-ae57d265620a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"9816731e-a4e5-4802-97f6-ae57d265620a","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/9c0837ef-f9ba-4a25-8778-93b8e0d4859b.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"9c0837ef-f9ba-4a25-8778-93b8e0d4859b","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/a1fc937b-559f-45d6-be36-7fbb61c00735.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"a1fc937b-559f-45d6-be36-7fbb61c00735","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/a73f04c2-5d4b-44dd-a390-b675daa7e314.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"a73f04c2-5d4b-44dd-a390-b675daa7e314","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/b1e296d2-d8a2-4530-979d-99c6328a7f02.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"b1e296d2-d8a2-4530-979d-99c6328a7f02","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/b523b4fb-a225-4fd2-8d91-e3ba3d4752f9.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"b523b4fb-a225-4fd2-8d91-e3ba3d4752f9","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/baa30cb6-83a0-4d0b-a41e-d6800f7ce34d.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"baa30cb6-83a0-4d0b-a41e-d6800f7ce34d","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/c31d8a7e-4d79-4486-a202-2f0532c90339.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"c31d8a7e-4d79-4486-a202-2f0532c90339","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/d875e38c-8b3a-4f25-a1f7-26db117fa2af.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"d875e38c-8b3a-4f25-a1f7-26db117fa2af","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/d8da8660-677e-442c-a330-3e4b132aebb0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"d8da8660-677e-442c-a330-3e4b132aebb0","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/dadd0c64-7022-41f3-be4c-f2ce83ec2f96.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"dadd0c64-7022-41f3-be4c-f2ce83ec2f96","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/db4026c4-603e-493c-a538-279060297dd1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"db4026c4-603e-493c-a538-279060297dd1","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/e2a105fb-758c-4e28-8588-17ad9e32033a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"e2a105fb-758c-4e28-8588-17ad9e32033a","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/e98e4ca9-d402-4921-9097-c6c50dab666f.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"e98e4ca9-d402-4921-9097-c6c50dab666f","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/eb9d8df1-4f8e-4de5-8fc2-17b4031c45cf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"eb9d8df1-4f8e-4de5-8fc2-17b4031c45cf","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/f798dcfd-4171-49f6-a082-fe256dfce193.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"f798dcfd-4171-49f6-a082-fe256dfce193","name":"name","comicUni":"comicUni"}
1 change: 1 addition & 0 deletions data/superhero/undefined.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"message":"bad request"}
48 changes: 48 additions & 0 deletions lib/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'), {suffix: 'Prom'});
const createError = require('http-errors');
const debug = require('debug')('superhero:storage');

module.exports = exports = {};

exports.createItem = function(schemaName, item) {
debug('createItem');

if (!schemaName) return Promise.reject(createError(400, 'bad request'));
if (!item) return Promise.reject(createError(400, 'bad request'));

let json = JSON.stringify(item);
return fs.writeFileProm(`${__dirname}/../data/${schemaName}/${item.id}.json`, json)
.then( () => item)
.catch( err => Promise.reject(err));
};

exports.fetchItem = function(schemaName, id) {
debug('fetchItem');

if (!schemaName) return Promise.reject(createError(400, 'bad request'));
if (!id) return Promise.reject(createError(400, 'bad request'));

return fs.readFileProm(`${__dirname}/../data/${schemaName}/${id}.json`)
.then( data => {
debug('then block');
try {
let item = JSON.parse(data.toString());
return item;
} catch (err) {
return Promise.reject(err);
}
})
.catch( err => Promise.reject(createError(404, `${schemaName} not found`)));
};

exports.deleteItem = function(schemaName, id) {
debug('deleteItem');

if (!schemaName) return Promise.reject(new Error('expected schema name'));
if (!id) return Promise.reject(new Error('expected id'));

return fs.unlinkProm(`${__dirname}/../data/${schemaName}/${id}.json`);
};
Loading