-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
87 lines (69 loc) · 1.8 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
87
require('dotenv').config();
const express = require('express');
const expressLayouts = require('express-ejs-layouts');
const mongoose = require('mongoose');
const flash = require('connect-flash');
const session = require('express-session');
const passport = require('passport');
const Inst = require('./models/Institute');
const app = express();
require('./config/passport')(passport);
app.use(expressLayouts);
app.use('/assets', express.static('./assets'));
app.set('view engine', 'ejs');
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(
session({
secret: 'secret',
resave: true,
saveUninitialized: true,
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
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');
next();
});
app.post('/addInsti', async (req, res) => {
try {
console.log(req.body);
const { name, adminId, inst } = req.body;
console.log({ name, adminId, inst });
const institute = new Inst({
name: name,
admin: adminId,
inst: inst,
});
await institute.save();
res.json({
message: 'Saved Sucessfully',
});
} catch (err) {
res.status(500).json({
message: err,
});
}
});
app.use('/', require('./routes/index'));
app.use('/auth', require('./routes/auth'));
app.use((req, res) => {
res.render('error_404');
});
const PORT = process.env.PORT || 3000;
const DB_URI = process.env.MONGODB_URI;
mongoose
.connect(DB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
.then(() => {
console.log('Successfully connected to MongoDB');
app.listen(PORT, console.log(`Server running on PORT ${PORT}`));
})
.catch(err => console.log(err));