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
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
29 changes: 29 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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: [
'mocha',
'es6'
]
}))
.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']);
68 changes: 68 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const jsonParser = bodyParser.json();
const rooneyRouter = express.Router();
const fs = require('fs');
const stream = require('stream');

const dir = __dirname + '/../data';
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}

rooneyRouter.use(jsonParser);

app.use('/rooney', rooneyRouter);

rooneyRouter.get('/', (req, res) => {
res.send('GLORY MAN UNITED');
});

rooneyRouter.get('/:id', (req,res) => {
let id = req.params.id;
console.log('hello from id get route');
res.json({message: id.toUpperCase()});
});

rooneyRouter.post('/', (req, res) => {
console.log('hit post route');
let rooney = '';
req.on('data', (data) => {
rooney += data.toString();
});
req.on('end', () => {
let nextFile = (fs.readdirSync(dir)).length +1;
let file = fs.createWriteStream(dir + '/' + nextFile + '.json');
var bufferStream = new stream.PassThrough();
let inBuf = new Buffer(rooney);
bufferStream.end(inBuf);
bufferStream.pipe(file);
res.json({message: 'Wrote a new file'});
});
});

rooneyRouter.put('/:id', (req, res) => {
console.log('rooneyRouter put hit');
let id = req.params.id;
var stream = fs.createWriteStream(dir + '/' + id + '.json');
req.pipe(stream);
let message = 'successfully updated';
res.json({message});
});

rooneyRouter.delete('/', (req, res) => {
console.log('delete route hit');
let message = 'successfully deleted';
res.json({message});
});

app.get('/*', (req, res) => {
res.status(404).json({msg: 'not found'});
});



app.listen(3000, () => console.log('up on 3000'));
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 ]
41 changes: 41 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"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"
},
"dependencies": {
"body-parser": "^1.15.1",
"express": "^4.13.4"
}
}
92 changes: 92 additions & 0 deletions test/rest_api_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
'use strict';

const chai = require('chai');
const chaiHTTP = require('chai-http');
const expect = chai.expect;
chai.use(chaiHTTP);
const request = chai.request;
const fs = require('fs');

let testFile;

const dir = __dirname + '/../data';
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}

require(__dirname + '/../lib/server');

describe('rest api tests', () => {
beforeEach('read files in data', (done) => {
testFile = '{"test": "test"}';
done();
});

it('should post correctly', (done) => {
request('localhost:3000')
.post('/rooney')
.send(testFile)
.end((err, res) => {
expect(err).to.eql(null);
expect(res.body.message).to.eql('Wrote a new file');
expect(res).to.have.status(200);
done();
});
});
it('should delete correctly', (done) => {
request('localhost:3000')
.delete('/rooney')
.end((err, res) => {
expect(err).to.eql(null);
expect(res.body.message).to.eql('successfully deleted');
expect(res).to.have.status(200);
done();
});
});
it('should respond to a failed test', (done) => {
request('localhost:3000')
.get('/fail')
.end((err, res) => {
expect(err).to.not.eql(null);
expect(res).to.have.status(404);
done();
});
});
it('should process get request correctly', (done) => {
request('localhost:3000')
.get('/rooney')
.end((err, res) => {
expect(err).to.eql(null);
expect(res).to.have.status(200);
expect(res.text).to.eql('GLORY MAN UNITED');
done();
});
});

it('should turn special id get request to uppercase', (done) => {
request('localhost:3000')
.get('/rooney/wayne')
.end((err, res) => {
expect(err).to.eql(null);
expect(res).to.have.status(200);
expect(res.text).to.eql('{"message":"WAYNE"}');
done();
});
});

describe('put tests', () => {

it('should put properly', (done) =>
{
request('localhost:3000')
.put('/rooney/1')
.send('{"test": "test"}')
.end((err, res) => {
expect(err).to.eql(null);
expect(res.body.message).to.eql('successfully updated');
expect(res).to.have.status(200);
done();
});
});
});
});