-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
85 lines (69 loc) · 2.14 KB
/
index.js
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
const express = require('express');
const path = require('path');
const app = express();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const passport = require('passport');
const usersRoutes = require('./routes/user-routes');
const basicRoutes = require('./routes/basic-routes')
const adminRoutes = require('./routes/admin-routes');
const session = require('express-session');
const mongostore = require('connect-mongo')(session);
// body parser middle ware
app.use(bodyParser.urlencoded({ extended : false }));
app.use(bodyParser.json());
// dotenv file
const dotenv = require('dotenv');
dotenv.config();
// session middle ware
app.use(session({
secret : 'mysupersecret',
resave : false,
saveUninitialized : false,
store : new mongostore({ mongooseConnection : mongoose.connection }),
// cookie : { maxAge : 180 * 60 * 1000 }
}));
// Passport Config
require('./config/passport')(passport);
// Passport Middleware
app.use(passport.initialize());
app.use(passport.session());
// view engine setup
app.set('views', path.join(__dirname,'views'));
app.set('view engine','ejs');
app.use(express.static(path.join(__dirname,'public')));
// Express Messages middleware
app.use(require('connect-flash')());
app.use(function (req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});
app.get('*', function(req,res,next) {
res.locals.user = req.user || null;
next();
});
// basic routes
app.use('/' , basicRoutes);
//admin routes
app.use('/admin',adminRoutes);
// user routes
app.use('/users', usersRoutes);
app.use((req,res,next) => {
// req.flash("Invalid url");
res.redirect('/');
});
app.use((error, req, res, next) => {
if (res.headerSent) {
return next(error);
}
const status = error.status || 500;
res.status(status);
res.json({ message: error.message || 'An unknown error occurred!' });
});
mongoose
.connect(
process.env.DB,
{ useCreateIndex: true,useUnifiedTopology: true, useNewUrlParser: true },
)
.then(() => app.listen(process.env.PORT || 5000, console.log("Your server is up man.....")))
.catch((err) => console.log(err));