Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
8c07def
finished setting up
May 24, 2016
879b538
finished making CRUD operations
May 24, 2016
d1fc5e7
finished making CRUD operations
May 24, 2016
dc74c44
made the final tests work
May 25, 2016
cf34eb0
setup almost done
May 26, 2016
f181fed
made rooney file in schema
May 26, 2016
c59ff67
made rooney get request
May 26, 2016
09e6a01
made rooney post
May 26, 2016
84c3476
made rooney put
May 26, 2016
b58d92c
made rooney delete
May 26, 2016
ebbb76f
deleleted consts in server
May 26, 2016
1a979a9
made messi schema
May 26, 2016
838c1a8
made messi router setup
May 26, 2016
bdc6380
made messi router get request
May 26, 2016
3e8fd75
made messi router post request
May 26, 2016
4dd07b1
made messi router put request
May 26, 2016
d803610
made messi router delete request
May 26, 2016
9ff5d1e
made rooney router now a man united player router
May 26, 2016
e048b9c
made messi router a barca player router
May 26, 2016
39e0650
added position property to barca players
May 26, 2016
7a8ff40
added number and goals property to barca players
May 26, 2016
d4c3d71
added functionality to add total goals for each collection
May 27, 2016
75cd372
added most goals path and is working
May 27, 2016
9caaa86
started making manUnitedRoute file and modifying server and added err…
May 27, 2016
8c65448
finished modularizing
May 27, 2016
073ab87
finished deleting old commented out code in server and started tests
May 27, 2016
9e5fdac
declared consts in test
May 27, 2016
6984a8a
wrote united get test
May 27, 2016
7792315
wrote united post test
May 27, 2016
38fe85f
wrote united put test
May 27, 2016
effcbae
wrote next err for man u routes
May 27, 2016
7003eaf
wrote next err for barca routes
May 27, 2016
bf70da9
made man u delete tests
May 27, 2016
bc128a8
made barca tests
May 27, 2016
b8405ac
made compare route work again ready to start tests
May 27, 2016
3d2379f
started writing compare test but realized db is dropped evry time
May 27, 2016
984e12b
beginning compare test again
May 27, 2016
94cf2de
beginning compare test again
May 27, 2016
af739e9
made new test for compare
May 27, 2016
c1e8de6
put in notes for tics
May 28, 2016
78fa397
fixed linter errors
May 31, 2016
44a6d06
wrote user.js file and renamed schema folder to model
Jun 1, 2016
b14f614
wrote basic_http.js file
Jun 1, 2016
d4c595d
wrote auth_routes post request
Jun 1, 2016
9cbff38
wrote auth_routes login route
Jun 1, 2016
22cd0b5
updated server.js with auth route
Jun 1, 2016
138659f
changed schema to model in routes files
Jun 1, 2016
f6c68c0
changed schema to model in test also and started writing auth tessts
Jun 1, 2016
502e66d
declared neccessary consts for auth_test
Jun 1, 2016
5d9a0e3
wrote after block for authtests
Jun 1, 2016
f5bae51
wrote signup route test
Jun 1, 2016
3889bdb
wrote login route test
Jun 1, 2016
f3f3165
update package.json with jbt and started auth file
Jun 2, 2016
70a3e13
made jwt_auth file
Jun 2, 2016
b2756bf
updated user.js file with generate token method
Jun 2, 2016
22bd240
updated server.js with test routes
Jun 2, 2016
6471681
updated auth_routes with generate token function implemented
Jun 2, 2016
db4ba6d
error fix: changed req to res
Jun 2, 2016
2c393c5
token test file set up ready to write tests
Jun 2, 2016
daa1b4f
made change to server get test route so its test will work and is wor…
Jun 2, 2016
bc0b911
working on tests
Jun 2, 2016
0826652
got first route test towork for login
Jun 2, 2016
6d92d6e
made changes in package.json to scripts
Jun 3, 2016
176ab7b
fixed lintrc
Jun 3, 2016
1f40ee3
added token generator
Jun 5, 2016
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
41 changes: 41 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"rules": {
"no-console": 0,
"indent": [
2,
2
],
"quotes": [
2,
"single"
],
"linebreak-style": [
2,
"unix"
],
"semi": [
2,
"always"
]
},
"env": {
"es6": true,
"node": true,
"browser": true,
"mocha": true
},
"globals": {
"describe": false,
"it": false,
"beforeEach": false,
"afterEach": false,
"before": false,
"after": false
},
"ecmaFeatures": {
"modules": true,
"experimentalObjectRestSpread": true,
"impliedStrict": true
},
"extends": "eslint:recommended"
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
data
db
19 changes: 0 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1 @@
#REST API

##To Submit this Assignment
* fork this repository
* write all of your code in a folder containing your name
* push to your repository
* submit a pull request to this repository
* submit a link to your PR in canvas

##Description
Create a single resource (like /penguins or /seals) REST API with Express.

Write a full set of CRUD routes for your resource. You can persist it however you like for the sake of testing, we'll be connecting it to Mongo later in the week.


##Rubric
CRUD Routes: 4pts
Use of Express: 2pts
Tests: 2pts
Project Organization: 2pts
26 changes: 26 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const gulp = require('gulp');
const eslint = require('gulp-eslint');
const mocha = require('gulp-mocha');

gulp.task('lint', () => {
gulp.src(['./*.js', './test/*.js'])
.pipe(eslint({
env: {}
}))
.pipe(eslint.format());
});

gulp.task('mocha', () => {
return gulp.src('./test/*.js', {
read: false
})
.pipe(mocha({
reporter: 'nyan'
}));
});

gulp.task('watch', () => {
gulp.watch(['./*.js', './test/*.js' ], ['mocha', 'lint']);
});

gulp.task('default', ['mocha', 'lint', 'watch']);
21 changes: 21 additions & 0 deletions lib/basic_http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

module.exports = function (req, res, next) {
let basicAuth = req.headers.authorization;
let authString = basicAuth.split(' ').pop();
let authBuff = new Buffer(authString, 'base64');
let asciiAuth = authBuff.toString();
let authArray = asciiAuth.split(':');
authBuff.fill(0);

req.auth = {
username: authArray[0],
password: authArray[1]
};

if(!req.auth.username || !req.auth.password) {
return next(new Error ('Username or Password missing'));
}

next();
};
5 changes: 5 additions & 0 deletions lib/error_handling.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = function(err, req, res) {
res.status(500).json({message: err.message});
};
25 changes: 25 additions & 0 deletions lib/jwt_auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const jwt = require('jsonwebtoken');
const User = require('../model/user');
const secret = process.env.SECRET || 'changeme';

module.exports = function (req, res, next) {
let token = req.body.token || req.headers.token;
let tokenErr = new Error('Authorization Failure');
let decodedToken;

if (!token) return next(tokenErr);

try {
decodedToken = jwt.verify(token, secret);
} catch(e) {
return next(tokenErr);
}

User.findOne({_id:decodedToken._id}, (err, user) => {
if (!user || err) return next(tokenErr);
req.user = user;
next();
});
};
12 changes: 12 additions & 0 deletions model/barca_Player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

const mongoose = require('mongoose');

const BarcaPlayer = new mongoose.Schema({
name: String,
position: String,
number: Number,
goals: Number
});

module.exports = mongoose.model('barca', BarcaPlayer);
12 changes: 12 additions & 0 deletions model/man_United_Player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

const mongoose = require('mongoose');

const ManUnitedPlayer = new mongoose.Schema({
name: String,
position: String,
number: Number,
goals: Number
});

module.exports = mongoose.model('manUnitedPlayer', ManUnitedPlayer);
25 changes: 25 additions & 0 deletions model/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const secret = process.env.SECRET || 'changeme';
const jwt = require('jsonwebtoken');

const User = new mongoose.Schema({
username: {type: String, required: true},
password: {type: String, required: true}
});

User.methods.hashPassword = function() {
return bcrypt.hashSync(this.password, 8);
};

User.methods.comparePassword = function(password) {
return bcrypt.compareSync(password, this.password);
};

User.methods.generateToken = function() {
return jwt.sign({_id: this._id}, secret);
};

module.exports = mongoose.model('user', User);
43 changes: 43 additions & 0 deletions npm-debug.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
0 info it worked if it ends with ok
1 verbose cli [ '/Users/drewferris/.node/bin/node',
1 verbose cli '/Users/drewferris/.node/bin/npm',
1 verbose cli 'run',
1 verbose cli 'lint' ]
2 info using npm@2.15.1
3 info using node@v4.4.3
4 verbose run-script [ 'prelint', 'lint', 'postlint' ]
5 info prelint man-united@1.0.0
6 info lint man-united@1.0.0
7 verbose unsafe-perm in lifecycle true
8 info man-united@1.0.0 Failed to exec lint script
9 verbose stack Error: man-united@1.0.0 lint: `./node_modules/eslint/bin/eslint.js *`
9 verbose stack Exit status 1
9 verbose stack at EventEmitter.<anonymous> (/Users/drewferris/.node/lib/node_modules/npm/lib/utils/lifecycle.js:217:16)
9 verbose stack at emitTwo (events.js:87:13)
9 verbose stack at EventEmitter.emit (events.js:172:7)
9 verbose stack at ChildProcess.<anonymous> (/Users/drewferris/.node/lib/node_modules/npm/lib/utils/spawn.js:24:14)
9 verbose stack at emitTwo (events.js:87:13)
9 verbose stack at ChildProcess.emit (events.js:172:7)
9 verbose stack at maybeClose (internal/child_process.js:827:16)
9 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)
10 verbose pkgid man-united@1.0.0
11 verbose cwd /Users/drewferris/401/my_assignmnents/drew_rest_api
12 error Darwin 14.5.0
13 error argv "/Users/drewferris/.node/bin/node" "/Users/drewferris/.node/bin/npm" "run" "lint"
14 error node v4.4.3
15 error npm v2.15.1
16 error code ELIFECYCLE
17 error man-united@1.0.0 lint: `./node_modules/eslint/bin/eslint.js *`
17 error Exit status 1
18 error Failed at the man-united@1.0.0 lint script './node_modules/eslint/bin/eslint.js *'.
18 error This is most likely a problem with the man-united package,
18 error not with npm itself.
18 error Tell the author that this fails on your system:
18 error ./node_modules/eslint/bin/eslint.js *
18 error You can get information on how to open an issue for this project with:
18 error npm bugs man-united
18 error Or if that isn't available, you can get their info via:
18 error
18 error npm owner ls man-united
18 error There is likely additional logging output above.
19 verbose exit [ 1, true ]
45 changes: 45 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "man-united",
"version": "1.0.0",
"description": "Class Projest- Router",
"main": "index.js",
"directories": {
"test": "mocha"
},
"scripts": {
"test": "./node_modules/mocha/bin/mocha",
"start": "node server.js",
"lint": "./node_modules/eslint/bin/eslint.js *"
},
"repository": {
"type": "git",
"url": "git+https://github.com/drewferris/man-united-router.git"
},
"keywords": [
"router",
"server"
],
"author": "Drew Ferris",
"license": "ISC",
"bugs": {
"url": "https://github.com/drewferris/man-united-router/issues"
},
"homepage": "https://github.com/drewferris/man-united-router#readme",
"devDependencies": {
"chai": "^3.5.0",
"chai-http": "^2.0.1",
"gulp": "^3.9.1",
"gulp-eslint": "^2.0.0",
"gulp-mocha": "^2.2.0",
"gulp-watch": "^4.3.5",
"mocha": "^2.4.5",
"mongoose": "^4.4.19",
"morgan": "^1.7.0"
},
"dependencies": {
"bcrypt": "^0.8.6",
"body-parser": "^1.15.1",
"express": "^4.13.4",
"jsonwebtoken": "^7.0.0"
}
}
31 changes: 31 additions & 0 deletions routes/auth_routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

const express = require('express');
const bodyParser = require('body-parser').json();
const User = require('../model/user');
const basicHTTP = require('../lib/basic_http');

const router = module.exports = exports = express.Router();

router.post('/signup', bodyParser, (req, res, next) => {
let newUser = new User(req.body);
let hashedPassword = newUser.hashPassword();
newUser.password = hashedPassword;
req.body.password = null;
User.findOne({username: req.body.username}, (err, user) => {
if(err || user) return next(new Error('could not create user'));
newUser.save((err, user) => {
if (err) return next(new Error('could not create user'));
res.json({token: user.generateToken()});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good Job!

});
});
});

router.get('/login', basicHTTP, (req, res, next) => {
User.findOne({username: req.auth.username}, (err, user) => {
if(err || !user) return next(new Error('Could not login'));
if (!user.comparePassword(req.auth.password)) return next('Could not sign in');

res.json({token: user.generateToken()});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good Job!

});
});
41 changes: 41 additions & 0 deletions routes/barca_routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const express = require('express');
const bodyParser = require('body-parser').json();
const BarcaPlayer = require('../model/barca_Player');

const router = module.exports = exports = express.Router();

router.get('/', (req, res) => {
BarcaPlayer.find({}, (err, data) => {
if(err) return res.json({
message: err.message
});
res.json(data);
});
});

router.post('/', bodyParser, (req, res) => {
let newBarcaPlayer = new BarcaPlayer(req.body);
newBarcaPlayer.save((err, data) => {
if(err) return res.json({message: err.message});
res.json(data);
});
});

router.put('/', bodyParser, (req, res, next) => {
BarcaPlayer.findOneAndUpdate({_id: req.body._id}, req.body, (err) => {
if(err) return next(err);
let message = 'successfully updated';
res.json({message});
});
});

router.delete('/:id', bodyParser, (req, res, next) => {
let _id = req.params.id;
BarcaPlayer.findOneAndRemove({_id}, null, (err) => {
if(err) return next(err);
let message = 'successfully deleted';
res.json({message});
});
});
Loading