-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
144 lines (112 loc) · 3.94 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
require('dotenv').config()
const express = require('express')
const expressLayouts = require('express-ejs-layouts');
const flash = require('connect-flash') // Connect flash
const session = require('express-session') // Connect session
const passport = require('passport')
const webpush = require('web-push');
const bodyParser = require('body-parser');
const path = require("path");
const mongo = require('./dbconnect')
var mongoose = require('mongoose');
// Expert Model
const Expert = require('./models/Expert')
// User Model
const User = require('./models/User')
const app = express();
// // Body parser middleware
app.use(bodyParser.json());
// // Push
const publicVapidKey = "BOHjA_8rowVAwIEwllMoc5ha_NJEYepUvXAyArv4886enG1hzvIxGSn-v3Lr3koyDlvYxmU1M8HKgbvcHhTc95Y";
const privateVapidKey = "Jj7jBQ2Q9kdKFEqJC5-KtTTMN8SK8bzLIMcexm8q3rQ";
webpush.setVapidDetails('mailto:push@saife.com', publicVapidKey, privateVapidKey);
// Subscribe Route
app.post('/subscribe', (req, res) => {
console.log(res.statusCode);
// Get pushSubscription object from client
const subscription = req.body;
// Send 201 - resource created
// res.status(201).json({});
// console.log(JSON.stringify(subscription));
// Save the subscription object in the database
console.log(req.body)
if(req.body.isExpert==='true'){
expertId = new mongoose.mongo.ObjectId(subscription.id);
Expert.findByIdAndUpdate(expertId,
{ pushSubObj: subscription.pushSubObj }, function (err, data) {
if (err) {
console.log(err);
}
else {
// res.send(data);
// console.log("pushSubObj updated! id : ", userId);
console.log("Im expert")
}
});
}else{
userId = new mongoose.mongo.ObjectId(subscription.id);
User.findByIdAndUpdate(userId,
{ pushSubObj: subscription.pushSubObj }, function (err, data) {
if (err) {
console.log(err);
}
else {
// res.send(data);
// console.log("pushSubObj updated! id : ", userId);
}
});
}
res.status(201).json({ statusMessage: "Subscribed" });
})
// EJS
app.use(expressLayouts);
app.set('view engine', 'ejs');
// Express body parser
app.use(express.urlencoded({ extended: true }));
// Passport Config
require('./config/passport')(passport)
// Connect to mongo
mongo();
// EJS
app.use(expressLayouts);
app.set('view engine', 'ejs');
// Bodyparser (can get data from form using request.body)
app.use(express.urlencoded({ extended: true }))
// Express session middleware
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true
}))
// Passport middleware
app.use(passport.initialize());
app.use(passport.session());
// Connect flash
app.use(flash())
// Global Vars
app.use((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()
})
const PORT = process.env.PORT || 3000
// Static files access
app.use(express.static(path.join(__dirname, 'public')))
app.use(express.static(path.join(__dirname, 'views')))
// Routes
app.use('/', require('./routes/index.js'));
app.use('/users', require('./routes/users.js'));
// app.use('/experts',require('./routes/experts.js'));
// app.use('/myMembers', require('./routes/members.js'));
// https://stackoverflow.com/questions/56308581/uncaught-error-listen-eaddrinuse-address-already-in-use-3000-when-mocha-uni
if (!module.parent) {
app.listen(PORT, () =>
console.log(`server listening on port ${PORT}!`),
);
}
// app.listen(PORT,console.log(`server started on port ${PORT}`));
// app.listen(5000,'192.168.0.177',function(){
// console.log('Server running at http://127.0.1.1:8000/')
// })
module.exports = app;