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

check #27

Open
wants to merge 2 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
Jump to file
Failed to load files.
Loading
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
86 changes: 86 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const express = require('express');
const app = express();

//body parser
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));

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

// session
const session = require("express-session");
app.use(
session({
secret: "123456",
resave: false,
saveUninitialized: true
})
);

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

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

//logging
const morgan = require('morgan');
app.use(morgan('tiny'));

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

// hbs
const expressHandlebars = require('express-handlebars');
const hbs = expressHandlebars.create({
partialsDir: 'views/',
defaultLayout: 'application'
});

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

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

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

// server
var port = process.env.PORT ||
process.argv[2] ||
3000;
var host = 'localhost';

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

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

app.listen.apply(app, args);

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": "assignment_thoreddit_development",
"host": "localhost"
},
"test": {
"database": "assignment_thodreddit_test",
"host": "localhost"
},
"production": {
"use_env_variable": "MONGODB_URI"
}
}
11 changes: 11 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var mongoose = require('mongoose');
var bluebird = require('bluebird');

mongoose.Promise = bluebird;

var models = {};

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

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

var PostSchema = new Schema({
title: String,
body: String,
user: {
type: Schema.Types.ObjectId,
ref: 'User'
}
}, {
timestamps: true
});

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

module.exports = Post;
16 changes: 16 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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;
11 changes: 11 additions & 0 deletions mongo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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, {useNewUrlParser: true});
};
Loading