This repository has been archived by the owner on Apr 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
133 lines (119 loc) · 4.1 KB
/
app.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
let fs = require('fs')
//HTTPS credentials:
//Express set up
let express = require("express");
//var http = require('http')
//var https = require('https');
app = express();
serverPort = 8000;
io = require('socket.io')();
router = require('express-router');
//Passport JS setup
let passport = require('passport');
LocalStrategy = require('passport-local').Strategy;
//BodyParser set up
bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: true }));
//Mongoose set up
mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/Woof");
//Models settings
Dog = require("./models/dog");
User = require("./models/user");
//MethodOverride set up
let methodOverride = require('method-override')
app.use(methodOverride('_method'))
//Seed file set up and run!
seed = require("./models/seed");
seed();
//App config
app.use(require('express-session')({
secret: 'I wanna go poopiee',
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
//Route config
app.use('/public', express.static(__dirname + '/public'));
app.use(require('./routes/dogOwners'));
app.use(require('./routes/landing'));
app.use(require('./routes/loginAndRegister'));
app.use(require('./routes/map'));
app.use(require('./routes/dog'));
//App settings
app.set('port', serverPort);
app.set('view engine', 'ejs');
app.set('views', 'views');
app.set('sockets', []);
var server = '';
if (process.argv[2] == 'live') {
server = app.listen(app.get('port'), app.get('host'), function() {
console.log('Listening on host' + app.get('host') + ', port ' + app.get('port'));
});
/* let pkey = fs.readFileSync('ssl/server.key');
let pcert = fs.readFileSync( 'ssl/mydomain.csr' )
var credentials = {key: pkey, cert: pcert};
var httpsServer = https.createServer(credentials, app).listen(app.get('port'));
*/
app.set('isLocal', false);
} else {
server = app.listen(app.get('port'), function() {
console.log('Listening on port ' + app.get('port'));
});
/*var httpServer = http.createServer(app).listen(app.get('port'));
*/
app.set('isLocal', true);
console.log('serving on local host ')
}
//*************SOCKET code*******************
io.attach(server);
let sockets = []
io.on('connection', function(socket) {
console.log('connection callback called.');
socket.emit('CONNECTED_USERS_INFO', { 'connected': sockets });
socket.broadcast.emit('SPECIAL_MESSAGE_SENT', { 'message': `Someone connected!` })
socket.on('POSITION_RECEIVED', function(latLng) {
//sockets.push(0);
console.log('position received.');
//Note: latLng is a json object of :
//{lat: LATITUDE, lng: LONGITUDE};
let coordinates = [Number(latLng.lng), Number(latLng.lat)];
let location = coordinates; //[34.0689, -118.4452];
console.log('my coordinates:' + coordinates)
let tolerance = 1;
Dog.find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: coordinates
}
}
}
}, function(error, foundPups) {
if (error) {
console.log("GEO-SPATIAL QUERRY FAILED")
console.log(error);
} else {
console.log(foundPups);
console.log("FOUND DOGS");
//WILSON !!
//foundPups is an array of Dog objects
//pup below is each dog .location gives you the coordinates
foundPups.forEach(function(pup) {
if (typeof pup.location !== "undefined") {
console.log(pup.location);
}
socket.emit('DOGS_NEAR_USER', foundPups);
});
}
});
});
socket.on('SEND_MESSAGE', function(message) {
socket.broadcast.emit('MESSAGE_SENT', message);
});
});