-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
372 lines (311 loc) · 11.5 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
server.js
Starting point of the node.js server appliction
Revision history
Hemanth Kona, 2014.06.19: created
Hemanth Kona, 2014.06.20: connection to mongodb database is established using mongoose
Hemanth Kona, 2014.06.21: defined User schemas
Hemanth Kona, 2014.06.21: user authentication implemented using passport
Hemanth Kona, 2014.06.22: defined project schemas
Hemanth Kona, 2014.06.23: project data persisted to mongodb
Hemanth Kona, 2014.06.23: defined create project route
Hemanth Kona, 2014.06.24: defined Show all projects route
Hemanth Kona, 2014.06.25: defined edit project route
*/
require('newrelic');
// import required modules
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var mongoose = require('mongoose');
var Project = require('./lib/models/project.js');
var User = require('./lib/models/user.js');
var docs = require("express-mongoose-docs");
// Initiating express server
var app = express();
// Authentication
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) next();
else res.send(401, { message: 'You are not logged in.'});
}
// User authentication using passport
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
// Authenticate using passport local strategy i.e. using email and password
passport.use(new LocalStrategy({ usernameField: 'email' }, function(email, password, done) {
User.findOne({ email: email }, function(err, user) {
if (err) return done(err);
if (!user) return done(null, false);
user.comparePassword(password, function(err, isMatch) {
if (err) return done(err);
if (isMatch) return done(null, user);
return done(null, false);
});
});
}));
//mongoose.connect('localhost/emd');
//mongoose.connect('mongodb://admin:[email protected]:10078/emd')
mongoose.connect('mongodb://admin:[email protected]:10066/app27162449')
mongoose.connection.on('error', function() {
console.error(' MongoDB Connection Error. Please make sure MongoDB is running.');
});
// Define middleware
app.set('port', process.env.PORT || 5000)
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(session({ secret: 'Authentic',
saveUninitialized: true,
resave: true }));
app.use(passport.initialize());
app.use(passport.session());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
//app.use(express.static(path.join(__dirname, 'MultiFormExample')));
app.use(express.static(path.join(__dirname, 'public')));
app.use(function(req, res, next) {
if (req.user) {
res.cookie('user', JSON.stringify(req.user));
}
next();
});
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
// Login route
app.post('/api/login', passport.authenticate('local'), function(req, res) {
res.cookie('user', JSON.stringify(req.user));
res.send(req.user);
});
// Logout route
app.get('/api/logout', function(req, res, next) {
req.logout();
res.send(200);
});
// Signup route
app.post('/api/signup', function(req, res, next) {
var user = new User({
email: req.body.email,
password: req.body.password,
firstname: req.body.firstname,
lastname: req.body.lastname
});
user.save(function(err) {
if (err) return next(err);
res.send(200, "User created");
});
});
app.put('/api/updatePassword', ensureAuthenticated, function (req, res, next) {
var user = req.user;
user.password = req.body.password;
user.save(function(err) {
if(err) return next(err);
res.send(200, "Password updated");
})
})
// Get user profile
app.get('/api/profile', ensureAuthenticated, function(req, res, next) {
User.findOne({email: req.user.email}, function(err, user) {
if(err) return next(err);
res.send(200, user);
})
})
//Edit profile
app.put('/api/profile', ensureAuthenticated, function(req, res, next) {
var userProfile = {};
//userProfile.email = req.user.email;
if(req.body.firstname) userProfile.firstname = req.body.firstname;
if(req.body.lastname) userProfile.lastname = req.body.lastname;
User.findOneAndUpdate({email: req.user.email}, userProfile, function(err, user) {
if(err) return next(err);
res.send(200, user);
})
})
app.delete('/api/profile/:id', ensureAuthenticated, function(req, res, next) {
console.log("Currecnt unser ID: " + req.params.id);
User.remove({email: req.params.id}, function(err) {
if(err) return next(err);
res.send(200);
})
})
// Retrieve all projects
app.get('/api/projects', ensureAuthenticated, function(req, res, next) {
Project.find({createdBy: req.user.email})
.sort('-createdOn')
.exec(function(err, projects) {
if(err) return next(err);
if(projects.length === 0) res.send(200, 'No project found');
res.send(200, projects);
});
});
// To generate api documentation for exress-mongoose
docs(app, mongoose);
// Create new project
app.post('/api/projects', ensureAuthenticated, function(req, res, next) {
var user = req.user.email;
var project = new Project({
name: req.body.name,
createdBy: user,
geolocation: {
latitude: req.body.latitude,
longitude: req.body.longitude
},
data: {
design: {
lineVoltage: req.body.lineVoltage,
impedanceOne: req.body.impedanceOne,
impedanceTwo: req.body.impedanceTwo,
impedanceThree: req.body.impedanceThree,
decrementFactor: req.body.decrementFactor,
growthFactor: req.body.growthFactor,
physicalGridCoefficient: req.body.physicalGridCoefficient,
irregularityFactor: req.body.irregularityFactor,
averageResistivity: req.body.averageResistivity,
immediateResistivity: req.body.immediateResistivity,
clearingTime: req.body.clearingTime,
substationLength: req.body.substationLength,
substationWidth: req.body.substationWidth,
widthSpacing: req.body.widthSpacing,
lengthSpacing: req.body.lengthSpacing,
earthRodLength: req.body.earthRodLength,
geometricSpacingFactor: req.body.geometricSpacingFactor
},
construction: {
estimatedFaultCurrent: req.body.estimatedFaultCurrent,
designFaultCurrent: req.body.designFaultCurrent,
conductorLength: req.body.conductorLength,
earthMatResistance: req.body.earthMatResistance,
gridConductorLength: req.body.gridConductorLength,
minEarthRodsNumber: req.body.minEarthRodsNumber,
increasedEarthRodsNumber: req.body.increasedEarthRodsNumber,
newGridConductorLength: req.body.newGridConductorLength,
totalLengthOfCopper: req.body.totalLengthOfCopper,
maxStepVoltage: req.body.maxStepVoltage,
tolerableStepVoltage: req.body.tolerableStepVoltage,
designGrade: req.body.designGrade,
maxGridPotentialRise: req.body.maxGridPotentialRise,
recommendation: req.body.recommendation,
comments: req.body.comments,
totalVoltage: req.body.totalVoltage,
maxStepVoltagePercent: req.body.maxStepVoltagePercent,
tolerableStepVoltagePercent: req.body.tolerableStepVoltagePercent,
}
}
});
project.save(function(err, project) {
if (err) return next(err);
User.findOne({email: user}, function(err, u) {
if(err) return next(err);
u.projects.push(project.id);
u.save();
res.send(200, project);
})
})
});
// To retrieve a single project detials
app.get('/api/projects/:id', ensureAuthenticated, function(req, res, next) {
Project.findById(req.params.id, function(err, project) {
if(err) return next(err);
if(!project) res.send(404, { message: 'Project doesnt exist' });
console.log('CalledMethod: ' + project);
res.send(project);
})
});
// To edit existing project
app.put('/api/projects/:id', ensureAuthenticated, function(req, res, next) {
var user = req.user.email;
var project = {
name: req.body.projectName,
createdBy: user,
data: {
design: {
lineVoltage: req.body.lineVoltage,
impedanceOne: req.body.impedanceOne,
impedanceTwo: req.body.impedanceTwo,
impedanceThree: req.body.impedanceThree,
decrementFactor: req.body.decrementFactor,
growthFactor: req.body.growthFactor,
physicalGridCoefficient: req.body.physicalGridCoefficient,
irregularityFactor: req.body.irregularityFactor,
averageResistivity: req.body.averageResistivity,
immediateResistivity: req.body.immediateResistivity,
clearingTime: req.body.clearingTime,
substationLength: req.body.substationLength,
substationWidth: req.body.substationWidth,
widthSpacing: req.body.widthSpacing,
lengthSpacing: req.body.lengthSpacing,
earthRodLength: req.body.earthRodLength,
geometricSpacingFactor: req.body.geometricSpacingFactor
},
construction: {
estimatedFaultCurrent: req.body.estimatedFaultCurrent,
designFaultCurrent: req.body.designFaultCurrent,
conductorLength: req.body.conductorLength,
earthMatResistance: req.body.earthMatResistance,
gridConductorLength: req.body.gridConductorLength,
minEarthRodsNumber: req.body.minEarthRodsNumber,
increasedEarthRodsNumber: req.body.increasedEarthRodsNumber,
newGridConductorLength: req.body.newGridConductorLength,
totalLengthOfCopper: req.body.totalLengthOfCopper,
maxStepVoltage: req.body.maxStepVoltage,
tolerableStepVoltage: req.body.tolerableStepVoltage,
designGrade: req.body.designGrade,
maxGridPotentialRise: req.body.maxGridPotentialRise,
recommendation: req.body.recommendation,
comments: req.body.comments,
totalVoltage: req.body.totalVoltage,
maxStepVoltagePercent: req.body.maxStepVoltagePercent,
tolerableStepVoltagePercent: req.body.tolerableStepVoltagePercent,
}
}
};
Project.findOneAndUpdate({_id: req.params.id}, project, function(err, project) {
if(err) return next(err);
res.send(project);
})
});
// To delete existing project
app.delete('/api/projects/:id', ensureAuthenticated, function(req, res, next) {
Project.remove({_id: req.params.id}, function(err) {
if(err) return next(err);
User.findOne({email: req.user.email}, function(err, user) {
if(err) return next(err);
var item = 'ObjectId("'+req.param.id+'")';
user.projects.pop(item);
user.save();
res.send(200);
})
})
});
// redirect every other which is not defined route
app.get('*', function(req, res) {
res.redirect('/#' + req.originalUrl);
});
// Error handling
app.use(function(err, req, res, next) {
console.error(err.stack);
//res.send(500, {message: "Internal server error, we are notified."});
res.send(500, { message: err.message });
});
//Listen on port 1000
app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});