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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
npm-debug.log
node_modules
.DS_Store
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# assignment_mad_lib_api
Serving up the madness with a Mad Lib API!

Name: Ian Halverson and Greg Filipczak
23 changes: 23 additions & 0 deletions api.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Listing

returns the resourcce with a default of 10 unless specified with a count query
get http://localhost:3000/api/v1/nouns/
get http://localhost:3000/api/v1/nouns/?count= //number they pass in

get http://localhost:3000/api/v1/verbs/
get http://localhost:3000/api/v1/verbs/?count= //number they pass in

get http://localhost:3000/api/v1/adjectives/
get http://localhost:3000/api/v1/adjectives/?count= //number they pass in

get http://localhost:3000/api/v1/adverbs/
get http://localhost:3000/api/v1/adverbs/?count= //number they pass in


Creating

return story with words inserted

post http://localhost:3000/api/v1/

// req.body will be equal to the story and list of words
250 changes: 250 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
const express = require("express");
const app = express();
const expressSession = require("express-session");
const flash = require("express-flash-messages");
const morgan = require("morgan");
const highlight = require("cli-highlight").highlight;
const mongoose = require("mongoose");
const passport = require("passport");
const User = require("./models").User;
const expressHandlebars = require("express-handlebars");
const helpers = require("./helpers");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const { localStrategy, bearerStrategy } = require("./strategies");
const methodOverride = require("method-override");
const getPostSupport = require("express-method-override-get-post-support");
const morganToolKit = require("morgan-toolkit")(morgan);

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

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

app.use((req, res, next) => {
res.locals.siteTitle = "The Maddest of Libs";
next();
});
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());

app.use(
expressSession({
secret: "delicious",
resave: false,
saveUninitialized: true
})
);

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

app.use(flash());

app.use(methodOverride(getPostSupport.callback, getPostSupport.options));

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

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

if (process.env.NODE_ENV !== "test") {
app.use(morgan("tiny"));
app.use(morganToolKit());
}

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

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

passport.use("local", localStrategy);
passport.use("bearer", bearerStrategy);

passport.serializeUser((user, done) => done(null, user.id));
passport.deserializeUser((id, done) => {
User.findById(id)
.then(user => done(null, user))
.catch(e => done(null, false));
});

const loggedInOnly = (req, res, next) => {
return req.user ? next() : res.redirect("/login");
};

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

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

app.get("/login", loggedOutOnly, (req, res) => {
res.render("sessions/new");
});

const onLogout = (req, res) => {
req.logout();
req.method = "GET";
res.redirect("/login");
};
app.get("/logout", loggedInOnly, onLogout);
app.delete("/logout", loggedInOnly, onLogout);

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

app.use("/", usersRouter);

app.use(
"/api/v1",
require("./routers/maddest_libs")(
passport.authenticate("bearer", { session: false })
)
);

app.get("/stories/new", loggedInOnly, (req, res) => {
res.render("stories/new");
});
app.post("/stories", (req, res) => {
let sentence;
let nouns;
let verbs;
let adjectives;
let adverbs;
const newServer = app.listen(3050);
const story = req.body.story;
let nounMatches = 0;
let nounMatch = /{{ noun }}/g.exec(story);
while (nounMatch) {
nounMatches++;
nounMatch = /{{ noun }}/g.exec(story);
}
let verbMatches = 0;
let verbMatch = /{{ verb }}/g.exec(story);
while (verbMatch) {
verbMatches++;
verbMatch = /{{ verb }}/g.exec(story);
}
let adjectiveMatches = 0;
let adjectiveMatch = /{{ adjective }}/g.exec(story);
while (adjectiveMatch) {
adjectiveMatches++;
adjectiveMatch = /{{ adjective }}/g.exec(story);
}
let adverbMatches = 0;
let adverbMatch = /{{ adverb }}/g.exec(story);
while (nounMatch) {
adverbMatches++;
adverbMatch = /{{ adverb }}/g.exec(story);
}
request.get(
"http://localhost:3050/api/v1/nouns",
{
count: nounMatches
},
(err, res, body) => {
nouns = body;
}
);
request.get(
"http://localhost:3050/api/v1/verbs",
{
count: verbMatches
},
(err, res, body) => {
verbs = body;
}
);
request.get(
"http://localhost:3050/api/v1/adjectives",
{
count: adjectiveMatches
},
(err, res, body) => {
adjectives = body;
}
);
request.get(
"http://localhost:3050/api/v1/adverbs",
{
count: adverbMatches
},
(err, res, body) => {
adverbs = body;
}
);
request.post(
"http://localhost:3050/api/v1/stories",
{
form: {
words: nouns.concat(verbs.concat(adjectives.concat(adverbs))),
sentence: story
}
},
(err, res, body) => {
sentence = body;
}
);
res.end(sentence);
});

const port = process.env.PORT || process.argv[2] || 3000;
const host = "0.0.0.0";

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);
}

app.use("/api", (err, req, res, next) => {
if (res.headersSent) {
return next(err);
}

if (err.stack) {
err = err.stack;
}
res.status(500).json({ error: err });
});

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;
28 changes: 28 additions & 0 deletions config/mongoose.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"development": {
"database": "assignment_mad_lib_api_development",
"host": "localhost"
},
"test": {
"database": "assignment_mad_lib_api_test",
"host": "localhost"
},
"production": {
"use_env_variable": "MONGO_URL"
}
}















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


const DebugHelper = {};


DebugHelper.debug = (arg) => {
return `<pre>${ JSON.stringify(arg, null, 2) }</pre>`;
};


module.exports = DebugHelper;












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


const FlashHelper = {};


FlashHelper.bootstrapAlertClassFor = function(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;




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



const SessionsHelper = {};



SessionsHelper.loginPath = () => '/login';
SessionsHelper.logoutPath = () => '/logout?_method=delete';




module.exports = SessionsHelper;




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

StoriesHelper.storiesPath = () => `/stories`;

module.exports = StoriesHelper;
Loading