-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
222 lines (175 loc) · 5.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
var express= require('express');
var app=express();
var port=process.env.PORT||8080;
var morgan= require('morgan');
var mongoose= require('mongoose');
var bodyParser = require('body-parser');
var router=express.Router();
var appRoutes= require('./app/routes/api')(router);
var path= require('path');
var expressValidator = require('express-validator');
var expressSession = require('express-session');
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(expressValidator());
app.use(expressSession({secret: 'secret', saveUninitialized: false, resave: false}));
app.use(express.static(__dirname+'/public'));
var multer = require('multer');
var upload = multer({ dest: './uploads' });
app.use('/api', appRoutes);
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/middlistdb', function(err){
if(err){
console.log("cannot connect the database");
}
else{
console.log("we are connected to our database");
}
});
app.use(function(req,res,next){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Content-Type, Authorization");
next();
});
app.get('*', function(req,res){
res.sendFile(path.join(__dirname+'/public/app/views/index.html'));
});
app.listen(port, function () {
console.log("running the server on port"+port);
});
// var express = require('express'),
// bodyParser = require('body-parser'),
// app = express(),
// mongoose = require('mongoose'),
// bcrypt = require('bcryptjs'),
// nev = require('../../index')(mongoose);
// mongoose.connect('mongodb://localhost/emailtest');
// // our persistent user model
// var User = require('./app/userModel');
// // sync version of hashing function
// var myHasher = function(password, tempUserData, insertTempUser, callback) {
// var hash = bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
// return insertTempUser(hash, tempUserData, callback);
// };
// // async version of hashing function
// myHasher = function(password, tempUserData, insertTempUser, callback) {
// bcrypt.genSalt(8, function(err, salt) {
// bcrypt.hash(password, salt, function(err, hash) {
// return insertTempUser(hash, tempUserData, callback);
// });
// });
// };
// // NEV configuration =====================
// nev.configure({
// persistentUserModel: User,
// expirationTime: 600, // 10 minutes
// verificationURL: 'http://localhost:8000/email-verification/${URL}',
// transportOptions: {
// service: 'Gmail',
// auth: {
// user: '[email protected]',
// pass: 'middlebury2017'
// }
// },
// hashingFunction: myHasher,
// passwordFieldName: 'pw',
// }, function(err, options) {
// if (err) {
// console.log(err);
// return;
// }
// console.log('configured: ' + (typeof options === 'object'));
// });
// nev.generateTempUserModel(User, function(err, tempUserModel) {
// if (err) {
// console.log(err);
// return;
// }
// console.log('generated temp user model: ' + (typeof tempUserModel === 'function'));
// });
// // Express stuff =========================
// app.use(bodyParser.urlencoded());
// app.get('/', function(req, res) {
// console.log("email sent");
// res.sendFile('index.html', {
// root: __dirname
// });
// });
// app.post('/', function(req, res) {
// var email = req.body.email;
// // register button was clicked
// if (req.body.type === 'register') {
// var pw = req.body.pw;
// var newUser = new User({
// email: email,
// pw: pw
// });
// nev.createTempUser(newUser, function(err, existingPersistentUser, newTempUser) {
// if (err) {
// return res.status(404).send('ERROR: creating temp user FAILED');
// }
// // user already exists in persistent collection
// if (existingPersistentUser) {
// return res.json({
// msg: 'You have already signed up and confirmed your account. Did you forget your password?'
// });
// }
// // new user created
// if (newTempUser) {
// var URL = newTempUser[nev.options.URLFieldName];
// nev.sendVerificationEmail(email, URL, function(err, info) {
// if (err) {
// return res.status(404).send('ERROR: sending verification email FAILED');
// }
// res.json({
// msg: 'An email has been sent to you. Please check it to verify your account.',
// info: info
// });
// });
// // user already exists in temporary collection!
// } else {
// res.json({
// msg: 'You have already signed up. Please check your email to verify your account.'
// });
// }
// });
// // resend verification button was clicked
// } else {
// nev.resendVerificationEmail(email, function(err, userFound) {
// if (err) {
// return res.status(404).send('ERROR: resending verification email FAILED');
// }
// if (userFound) {
// res.json({
// msg: 'An email has been sent to you, yet again. Please check it to verify your account.'
// });
// } else {
// res.json({
// msg: 'Your verification code has expired. Please sign up again.'
// });
// }
// });
// }
// });
// // user accesses the link that is sent
// app.get('/email-verification/:URL', function(req, res) {
// var url = req.params.URL;
// nev.confirmTempUser(url, function(err, user) {
// if (user) {
// nev.sendConfirmationEmail(user.email, function(err, info) {
// if (err) {
// return res.status(404).send('ERROR: sending confirmation email FAILED');
// }
// res.json({
// msg: 'CONFIRMED!',
// info: info
// });
// });
// } else {
// return res.status(404).send('ERROR: confirming temp user FAILED');
// }
// });
// });
// app.listen(8080);
// console.log('Express & NEV example listening on 8000...');