Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

done #22

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

done #22

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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
*.swp
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Gene Tinderholm
# assignment_thoreddit
A social news web application for Viking thunder Gods
74 changes: 74 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
let session = require('express-session');

var index = require('./routes/index');
var users = require('./routes/users');
const post = require('./routes/posts');

var app = express();
let mongoose = require('mongoose');

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

var hbs = require('hbs');
hbs.registerPartials(__dirname + '/views/partials');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
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'] }
));
app.use(cookieParser());
app.use(session({
secret:'314159',
resave: false,
saveUninitialize: true
}));
app.use(express.static(path.join(__dirname, 'public')));
app.use((req, res, next) => {
if (mongoose.connection.readyState) {
next();
} else {
require('./mongo')().then(() => next());
}
});

app.use('/', index);
app.use('/users', users);
app.use('/posts', post);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;
90 changes: 90 additions & 0 deletions bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('assignment-thoreddit:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
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": "assignment_thoreddit_development",
"host": "localhost"
},
"test": {
"database": "assignment_thoreddit_test",
"host": "localhost"
},
"production": {
"use_env_variable": "MONGODB_URI"
}
}
1 change: 1 addition & 0 deletions dfas.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
input
25 changes: 25 additions & 0 deletions models/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
let mongoose = require('mongoose');
let Schema = mongoose.Schema;

let CommentSchema = new Schema(
{
body: String,
childIds: [
{
type: Schema.Types.ObjectId,
ref: "Comment"
}
],
userId: {
type: Schema.Types.ObjectId,
ref: "User"
},
},
{
timestamps: true,
},
);

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

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

mongoose.Promise = bluebird;

let models = {};

models.User = require('./user');
models.Post = require('./post');
models.Comment = require('./comment');

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

let PostSchema = new Schema(
{
body: String,
title: String,
userId: {
type: Schema.Types.ObjectId,
ref: 'User'
},
childIds: [
{
type: Schema.Types.ObjectId,
ref: 'Comment'
},
],
score: Number
},
{
timestamps: true,
}
);

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

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

let UserSchema = new Schema(
{
username: String,
},
{
timestamps: true,
},
);

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

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

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