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 @@
node_modules/
workspace.code-workspace
notes.txt
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# assignment_thoreddit
A social news web application for Viking thunder Gods
Jared Gebel
Please Discuss
A reddit-like application using Express, Mongoose, and Handlebars.
167 changes: 167 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
const express = require('express');
const app = express();
const path = require('path');

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

// ----------------------------------------
// Static Resources
// Serve public folder
// ----------------------------------------
app.use('/public', express.static(__dirname + '/public'));
// ----------------------------------------
// Body Parser
// Must set up before logging package
// ----------------------------------------
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));

// ----------------------------------------
// Sessions/Cookies
// ----------------------------------------
const session = require("express-session");

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


// ----------------------------------------
// HTTP Method Overriding
// ----------------------------------------
// Require the two packages
const methodOverride = require('method-override');
const getPostSupport = require('express-method-override-get-post-support');

// Pass the callback and options from the support package
app.use(methodOverride(
getPostSupport.callback,
getPostSupport.options // { methods: ['POST', 'GET']}
));

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

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

// ----------------------------------------
// Routes
// ----------------------------------------
const posts = require('./routers/posts');
app.use('/', posts);

const users = require('./routers/users');
app.use('/users', users);

// is there a way to split routes between
// files without overwriting a previous route?
// aka could i use '/' for sessions and
// just put different routes in the other file
const sessions = require('./routers/sessions');
app.use('/sessions', sessions);

const comments = require('./routers/comments');
app.use('/comments', comments);

const votes = require('./routers/votes');
app.use('/votes', votes);

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

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

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


// ----------------------------------------
// Server
// ----------------------------------------
app.listen(3000, 'localhost', () => {
console.log(`Listening on port 3000. \n`);
});
// 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.locals.baseUrl = `http://${ host }:${ port }`;
// server.listen.apply(server, args);
// }


// ----------------------------------------
// Logging
// ----------------------------------------
var morgan = require('morgan');
app.use(morgan('tiny'));

// Output value of req.query, req.params, and req.body
app.use((req, res, next) => {
['query', 'params', 'body'].forEach((key) => {
if(req[key]) {
var capKey = key[0].toUpperCase() + key.substr(1);
var value = JSON.stringify(req[key], null, 2);
console.log(`${ capKey }: ${ value }`);
}
});
next();
});

// ----------------------------------------
// 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;
13 changes: 13 additions & 0 deletions config/mongo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"development": {
"database": "please_discuss_development",
"host": "localhost"
},
"test": {
"database": "please_discuss_test",
"host": "localhost"
},
"production": {
"use_env_variable": "MONGODB_URI"
}
}
20 changes: 20 additions & 0 deletions models/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const CommentSchema = new Schema({
text: String,
author: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
comments: [{
type: Schema.Types.ObjectId,
ref: 'Comment'
}]
}, {
timestamps: true
});

const Comment = mongoose.model('Comment', CommentSchema);

module.exports = Comment;
16 changes: 16 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var mongoose = require('mongoose');
var bluebird = require('bluebird');

// Set bluebird as the promise
// library for mongoose
mongoose.Promise = bluebird;

var models = {};

// Load models and attach to models here
models.User = require('./user');
models.Comment = require('./comment');
models.Post = require('./post');
models.Vote = require('./vote');

module.exports = models;
25 changes: 25 additions & 0 deletions models/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const deepPopulate = require('mongoose-deep-populate')(mongoose);

const PostSchema = new Schema({
title: String,
text: String,
author: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
comments: [{
type: Schema.Types.ObjectId,
ref: 'Comment'
}]
}, {
timestamps: true
});

// PostSchema.statics.handleVotes = function()
PostSchema.plugin(deepPopulate);

const Post = mongoose.model('Post', PostSchema);

module.exports = Post;
27 changes: 27 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const UserSchema = new Schema({
fname: String,
lname: String,
username: String,
email: String,
posts: [{
type: Schema.Types.ObjectId,
ref: 'Post'
}],
comments: [{
type: Schema.Types.ObjectId,
ref: 'Comment'
}],
votes: [{
type: Schema.Types.ObjectId,
ref: 'Vote'
}]
}, {
timestamps: true
});

const User = mongoose.model('User', UserSchema);

module.exports = User;
22 changes: 22 additions & 0 deletions models/vote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const findOrCreate = require('mongoose-find-or-create');

const VoteSchema = new Schema({
// 1 - Upvote, -1 - Downvote, 0 - No Vote
status: { type: Number, default: 0 },
user: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
post: String,
comment: String
}, {
timestamps: true
});

VoteSchema.plugin(findOrCreate);

const Vote = mongoose.model('Vote', VoteSchema);

module.exports = Vote;
13 changes: 13 additions & 0 deletions mongo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const mongoose = require('mongoose');
var env = process.env.NODE_ENV || 'development';
var config = require('./config/mongo')[env];




module.exports = () => {
var envUrl = process.env[config.use_env_variable];
var localUrl = `mongodb://${ config.host }/${ config.database }`;
var mongoUrl = envUrl ? envUrl : localUrl;
return mongoose.connect(mongoUrl);
};
Loading