index.js
1.79 KB
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
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var flash = require('connect-flash');
var session = require('express-session');
var passport = require('./config/passport');
var multer = require('multer');
//require('./config/passport');
var app = express();
// DB Setting
mongoose.set('useNewUrlParser', true);
mongoose.set('useFindAndModify', false);
mongoose.set('useCreateIndex', true);
mongoose.set('useUnifiedTopology', true);
// Connect DB environment variable
mongoose.connect('mongodb+srv://Mapmory_admin:admin@cluster0.ncnjj.mongodb.net/Project-Mapmory?retryWrites=true&w=majority');
// Store DB in the variable 'db'
var db = mongoose.connection;
// If DB is opened successfully
db.once('open', function(){
console.log('DB connected');
});
// if DB is failed to open
db.on('error', function(err){
console.log('DB ERROR : ', err);
});
// Settings
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
app.engine('html', require('ejs').renderFile);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(methodOverride('_method'));
app.use(flash());
app.use(session({
secret:'MySecret',
resave:true,
saveUninitialized:true
}));
// Passport
app.use(passport.initialize());
app.use(passport.session());
// Custom Middlewares
app.use(function(req,res,next){
res.locals.isAuthenticated = req.isAuthenticated();
res.locals.currentUser = req.user;
next();
});
// Routes
app.use('/', require('./routes/home'));
app.use('/posts', require('./routes/posts'));
app.use('/users', require('./routes/users'))
// Server
var port = 3000;
app.listen(port, function(){
console.log('server on! http://localhost:' + port);
});