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
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ----------------------------------------
# NPM
# ----------------------------------------

node_modules/
npm-debug.log


# ----------------------------------------
# ENV
# ----------------------------------------
.env


5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# assignment_mad_lib_api
Serving up the madness with a Mad Lib API!
Serving up the madness with a Mad Lib assignment_mad_lib_api
Jeffrey Dederick

Edwin Yung
262 changes: 262 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
const express = require('express');
const app = express();

// ----------------------------------------
// App Variables
// ----------------------------------------
app.locals.appName = 'Mad Libs';

// ----------------------------------------
// ENV
// ----------------------------------------
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}

// ----------------------------------------
// Body Parser
// ----------------------------------------
const bodyParser = require('body-parser');
app.use(bodyParser.json());

// ----------------------------------------
// Sessions/Cookies
// ----------------------------------------
const cookieParser = require('cookie-parser');
const cookieSession = require('cookie-session');

app.use(cookieParser());
app.use(
cookieSession({
name: 'session',
keys: [process.env.SESSION_SECRET || 'secret']
})
);

app.use((req, res, next) => {
res.locals.session = req.session;
next();
});

// ----------------------------------------
// Flash Messages
// ----------------------------------------
const flash = require('express-flash-messages');
app.use(flash());

// ----------------------------------------
// Method Override
// ----------------------------------------
const methodOverride = require('method-override');
const getPostSupport = require('express-method-override-get-post-support');

app.use(
methodOverride(
getPostSupport.callback,
getPostSupport.options // { methods: ['POST', 'GET'] }
)
);

// ----------------------------------------
// Referrer
// ----------------------------------------
app.use((req, res, next) => {
req.session.backUrl = req.header('Referer') || '/';
next();
});

// ----------------------------------------
// Public
// ----------------------------------------
app.use(express.static(`${__dirname}/public`));

// ----------------------------------------
// Logging
// ----------------------------------------
const morgan = require('morgan');
const morganToolkit = require('morgan-toolkit')(morgan, {
req: ['cookies' /*, 'signedCookies' */]
});

app.use(morganToolkit());

// ----------------------------------------
// Mongoose
// ----------------------------------------
const mongoose = require('mongoose');
app.use((req, res, next) => {
if (mongoose.connection.readyState) {
next();
} else {
require('./mongo')().then(() => next());
}
});

// ----------------------------------------
// Sessions
// ----------------------------------------
// Require passport, strategies and User model
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const BearerStrategy = require('passport-http-bearer').Strategy;
const User = require('./models').User;

// Initialize passport
app.use(passport.initialize());
app.use(passport.session());

// Create local strategy
const localStrategy = new LocalStrategy(
{
// Set username field to email
// to match form
usernameField: 'email'
},
(email, password, done) => {
// Find user by email
User.findOne({ email: email })
.then(user => {
// The user is valid if the password is valid
const isValid = user.validatePassword(password);

// If the user is valid pass the user
// to the done callback
// Else pass false
return done(null, isValid ? user : false);
})
.catch(e => done(null, false));
}
);

// Create the token bearer strategy
const bearerStrategy = new BearerStrategy((token, done) => {
// Find the user by token
User.findOne({ token: token })
.then(user => {
// Pass the user if found else false
return done(null, user || false);
})
.catch(e => done(null, false));
});

// Use the strategy middlewares
passport.use(localStrategy);
passport.use(bearerStrategy);

// Serialize and deserialize the user
// with the user ID
passport.serializeUser((user, done) => done(null, user.id));
passport.deserializeUser((id, done) => {
// Find the user in the database
User.findById(id)
.then(user => done(null, user))
.catch(e => done(null, false));
});

// ----------------------------------------
// Session Helper Middlewares
// ----------------------------------------

// Set up middleware to allow/disallow login/logout
const loggedInOnly = (req, res, next) => {
return req.user ? next() : res.redirect('/login');
};

const loggedOutOnly = (req, res, next) => {
return !req.user ? next() : res.redirect('/');
};

// Show login only if logged out
app.get('/login', loggedOutOnly, (req, res) => {
res.render('sessions/new');
});

// Allow logout via GET and DELETE
const onLogout = (req, res) => {
// Passport convenience method to logout
req.logout();

// Ensure always redirecting as GET
req.method = 'GET';
res.redirect('/login');
};

app.get('/logout', loggedInOnly, onLogout);

app.delete('/logout', loggedInOnly, onLogout);

// Create session with passport
app.post(
'/sessions',
passport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/login',
failureFlash: true
})
);

// ----------------------------------------
// Routes
// ----------------------------------------

const usersRouter = require('./routers/users')({
loggedInOnly,
loggedOutOnly
});
app.use('/', usersRouter);

const story = require('./routers/story');
app.use('/api/v1', story);

// ----------------------------------------
// Template Engine
// ----------------------------------------
const expressHandlebars = require('express-handlebars');
const helpers = require('./helpers');

const hbs = expressHandlebars.create({
helpers: helpers,
partialsDir: 'views/',
defaultLayout: 'application'
});

app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');

// ----------------------------------------
// Server
// ----------------------------------------
const port = process.env.PORT || process.argv[2] || 3000;
const host = 'localhost';

let args;
process.env.NODE_ENV === 'production' ? (args = [port]) : (args = [port, host]);

args.push(() => {
console.log(`Listening: http://${host}:${port}\n`);
});

if (require.main === module) {
app.listen.apply(app, args);
}

// Disable logging in test mode
if (process.env.NODE_ENV !== 'test') {
app.use(morgan('tiny'));
}

// ----------------------------------------
// Error Handling
// ----------------------------------------
app.use((err, req, res, next) => {
if (res.headersSent) {
return next(err);
}

if (err.stack) {
err = err.stack;
}
res.status(500).render('errors/500', { error: err });
});

module.exports = app;
1 change: 1 addition & 0 deletions config/.keep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.keep
13 changes: 13 additions & 0 deletions config/mongoose.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"development": {
"database": "assignment_mad_lib_api_development",
"host": "localhost"
},
"test": {
"database": "assignment_mad_lib_api_test",
"host": "localhost"
},
"production": {
"use_env_variable": "MONGO_URL"
}
}
3 changes: 3 additions & 0 deletions curl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
curl -H "Authorization: Bearer 4185bb480d198be788edef9cd33057ef" -H "Content-Type: application/json" -d '{"sentence": "I like New York City and I love running and swimming"}' http://localhost:3000/api/v1/story

curl -H "Content-Type: application/json" -d '{"sentence": "I like New York City and I love running and swimming"}' http://localhost:3000/api/v1/story?access_token=4185bb480d198be788edef9cd33057ef
19 changes: 19 additions & 0 deletions helpers/application_helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const ApplicationHelper = {};


ApplicationHelper.rootPath = () => '/';
ApplicationHelper.CSSID = (...args) => {
let n = args.length;
let options = args[n - 1];
let viewPath = options.data.exphbs.view;
let id = viewPath.split('/').join('-');
return id;
};


module.exports = ApplicationHelper;





13 changes: 13 additions & 0 deletions helpers/bootstrap_helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const BootstrapHelper = {};


BootstrapHelper.glyphicon = type => {
return `<span
class="glyphicon
glyphicon-${ type }"
aria-hidden="true"></span>`
};



module.exports = BootstrapHelper;
14 changes: 14 additions & 0 deletions helpers/flash_helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const FlashHelper = {};


FlashHelper.bootstrapAlertClassFor = key => {
return {
"error": "danger",
"alert": "danger",
"notice": "info"
}[key] || key;
};


module.exports = FlashHelper;

9 changes: 9 additions & 0 deletions helpers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const LoadHelpers = require('load-helpers');
const helperLoader = new LoadHelpers();
const helpers = helperLoader.load('helpers/*_helper.js').cache;

module.exports = helpers;




19 changes: 19 additions & 0 deletions helpers/lodash_helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const _ = require('lodash');


const LodashHelper = {};


LodashHelper.isEmpty = _.isEmpty;
LodashHelper.size = _.size;



module.exports = LodashHelper;







14 changes: 14 additions & 0 deletions helpers/math_helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

const MathHelper = {};


MathHelper.add = (a, b) => a + b;
MathHelper.sub = (a, b) => a - b;
MathHelper.div = (a, b) => a / b;
MathHelper.mult = (a, b) => a * b;



module.exports = MathHelper;


Loading