-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
110 lines (95 loc) · 2.67 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
var express = require('express');
var app = express();
var router = express.Router();
var mongoose = require('mongoose');
var bodyparser = require('body-parser');
var User = require('./models/user');
var hash = require('./my_modules/smallfunc');
var session = require('client-sessions');
var port = process.env.PORT || 8000;
// var cors = require('cors');
// app.use(cors());
app.all('/', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.use(express.static(__dirname + '/public'));
app.use(bodyparser.urlencoded({ extended : true }));
app.use(session({
cookieName: 'session',
secret: 'g-mapster',
duration: 30 * 60 * 1000,
activeDuration: 5 * 60 * 1000,
}));
app.set('view engine', 'ejs');
mongoose.connect('mongodb://sumedh:[email protected]:39448/gmapster');
router.get('/', function(req, res){
res.redirect('/user');
});
app.route('/login')
.get(function(req, res){
res.render('pages/login');
})
.post(function(req, res){
User.find({ email : req.body.email }, function(err, user){
if(err)
throw err
else{
if(user[0].password == hash(req.body.password)){
req.session.user = user[0];
res.redirect('/home');
}
else{
res.render('pages/login');
}
}
});
});
// Signing up of a user
app.route('/signup')
.get(function(req, res){
res.render('pages/signup');
})
.post(function(req, res){
console.log(req.body.firstname, req.body.lastname);
var newUser = new User({
firstname : req.body.firstname,
lastname : req.body.lastname,
email : req.body.email,
admin : false
});
newUser.password = newUser.create_hash(req.body.password);
newUser.save(function(err){
if(err)
console.error(err);
console.log('new user saved successfully');
});
res.redirect('/signup');
});
router.get('/user', function(req, res){
res.header('Access-Control-Allow-Origin', '*');
res.render('pages/user');
});
router.get('/home', function(req, res){
res.render('pages/home');
});
router.get('/logout', function(req, res){
req.session.destroy();
res.send('Logged out successfully');
});
// var allowCrossDomain = function(req, res, next) {
// res.header('Access-Control-Allow-Origin', '*');
// res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
// res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
// // intercept OPTIONS method
// if ('OPTIONS' == req.method) {
// res.send(200);
// }
// else {
// next();
// }
// };
app.use('/', router);
app.listen(port);
console.log('server started');