-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js.save
106 lines (81 loc) · 2.68 KB
/
app.js.save
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const express = require('express');
const engine = require('ejs-mate');
const morgan = require('morgan')
const path = require('path');
const expressSanitizer = require('express-sanitizer');
const mongoose = require("mongoose");
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const fetch = require('node-fetch');
const flash = require('connect-flash');
const session = require('express-session');
const passport = require('passport');
const methodOveride = require('method-override');
var MongoDBStore = require('connect-mongodb-session')(session);
const app = express();
var store = new MongoDBStore({
uri: process.env.DATABASEURL,
collection: 'session'
});
require('./config/passport');
// const seedDB = require('./seeds');
// seedDB();
app.use(morgan('dev'));
mongoose.Promise = global.Promise;
mongoose.connect(process.env.DATABASEURL, { useNewUrlParser: true});
//mongoose.connect('mongodb://localhost:27017/sgfinal', { useNewUrlParser: true });
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
console.log('we\'re connected!');
});
// View Engine
app.engine('ejs', engine);
app.locals.moment = require('moment');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(methodOveride("_method"))
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressSanitizer());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
secret: 'gayassshit',
saveUninitialized: false,
resave: false,
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use((req, res, next)=> {
res.locals.currentUser = req.user;
res.locals.isAuthenticated = req.user ? true : false;
res.locals.success = req.flash('success');
res.locals.error = req.flash('error');
res.locals.page = '';
next();
});
app.use('/', require('./routes/index'));
app.use('/users', require('./routes/users'));
app.use('/services', require('./routes/services'));
app.use("/services/:id/reviews", require('./routes/reviews'));
app.use("/", require('./routes/admin'))
// catch 404 and forward to error handler
app.use((req, res, next) => {
res.status(404);
// respond with html page
if (req.accepts('html')) {
res.render('404', { url: req.url });
return;
}
// respond with json
if (req.accepts('json')) {
res.send({ error: 'Not found' });
return;
}
// default to plain-text. send()
res.type('txt').send('Not found');
});
app.listen(process.env.PORT || 3000, function(){
console.log("The YelpCamp Server Has Started!");
});