-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
86 lines (59 loc) · 1.93 KB
/
server.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
86
/* Including Express */
const express = require('express');
/* Firing Express to Include Modules to app */
const app = express();
/* Including body parser */
const bodyParser = require('body-parser');
/* Including mongoose for communicating to data base */
const mongoose = require('mongoose');
/* Including passport for authentication */
const passport = require('passport');
/* Including flash for message display */
const flash = require('connect-flash');
/* Including fast2sms for sending sms */
const fast2sms = require('fast-two-sms');
/* Including express sessions */
const session = require('express-session');
/* Adding path */
const path = require('path');
/* Setting Templating engine */
app.set('view engine' , 'ejs');
/* Passport Config */
require('./config/passport')(passport);
let urlencodedParser = bodyParser.urlencoded({ extended: false });
/* Database config */
require('dotenv').config();
const uri = process.env.mongodburl;
/* Connect to MongoDB */
mongoose.connect(uri,{ useNewUrlParser: true,useUnifiedTopology: true,useFindAndModify: false});
/* Express body parser */
app.use(express.urlencoded({ extended: true }));
/* Express session */
app.use(
session({
secret: 'secret',
resave: true,
saveUninitialized: true
})
);
/* Passport middleware */
app.use(passport.initialize());
app.use(passport.session());
/* Middleware to connect flash */
app.use(flash());
// Global variables
app.use(function(req, res, next) {
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.error = req.flash('error');
res.locals.success = req.flash('success');
next();
});
/* Access to static files */
app.use(express.static(path.join(__dirname + '/public')));
/* Routes */
app.use('/', require('./routes/users.js'));
const port = process.env.PORT || 4000;
app.listen(port,()=>{
console.log(`Server is running at http://localhost:${port}`);
});