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

Checking my code against the solution #23

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
64 changes: 64 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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');

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

var app = express();

// view engine setup
const expressHandlebars = require('express-handlebars');


const hbs = expressHandlebars.create({
partialsDir: 'views/',
defaultLayout: 'layout'
});
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

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

// 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 }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

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

// 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');
});
app.listen(3000, () => console.log('*******'))
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": "THOREDDIT_development",
"host": "localhost"
},
"test": {
"database": "THOREDDIT_test",
"host": "localhost"
},
"production": {
"use_env_variable": "MONGODB_URI"
}
}
1 change: 1 addition & 0 deletions models/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
18 changes: 18 additions & 0 deletions models/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var CommentSchema = new Schema({
title: String,
body: String,
username: String,
postedAt: Date,
CommentId: Number,
postRef: [{ type: Schema.Types.ObjectId, ref: 'Post' }]
},
{timestamps:true}
);

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


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


var CommentableSchema = new Schema({
comments: [{
type: Schema.Types.ObjectId,
ref: "Rating"
}]
}, {
timestamps: true,
discriminatorKey: 'kind'
});


var Commentable = mongoose.model('Commentable', CommentableSchema);




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


mongoose.Promise = bluebird;

var models = {};
models.User = require('./user');
models.Post = require('./post');


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

var PostSchema = new Schema({
title: String,
body: String,
username: String,
postedAt: Date,
postId: Number,
commentRef: [{ type: Schema.Types.ObjectId, ref: 'Comment' }]
},
{timestamps:true}
);

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


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


var UserSchema = new Schema({
fname: String,
lname: String,
username: String,
email: String
},

{

timestamps: true

})



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

module.exports = User;
23 changes: 23 additions & 0 deletions models/userpost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

var mongoose = require('mongoose');
var Schema = mongoose.Schema;


var UserPostSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: "User"
},
post: {
type: Schema.Types.ObjectId,
ref: "Post"
},
value: String
}, {
timestamps: true
});


var UserPost = mongoose.model('UserPost', UserPostSchema);

module.exports = UserPost;
10 changes: 10 additions & 0 deletions mongo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var 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);
};
1 change: 1 addition & 0 deletions node_modules/.bin/handlebars

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/semver

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/uglifyjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading