-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
94 lines (80 loc) · 2.53 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
var express = require('express.io'),
path = require('path'),
app = module.exports = express().http().io(),
mongoose = require('mongoose');
//dbauth = require('./dbauth');
mongoose.connect('mongodb://localhost/mianode');
app.configure(function(){
app.set('port', process.env.PORT || 5000);
app.set('views', __dirname + '/views' );
app.set('view engine', 'jade');
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.static(path.join(__dirname , '/public')));
});
var db = mongoose.connection;
db.on('error', function(){
console.log('connection error to db ...\n');
});
db.once('open', function(){
console.log('connection successful to db');
});
var locSchema = new mongoose.Schema({
name: String,
id: String,
ipAdd: String,
latlng: { lat: Number, lng: Number },
userAgent: String,
timestamp: { type: Date, default: Date.now }
},
{ collection: 'users' },
{ safe: 'safe' });
var location = mongoose.model('location', locSchema);
app.io.configure(function() {
app.io.enable('browser client minification'); // send minified client
app.io.enable('browser client gzip'); // gzip the file
//app.io.set('log level', 1); // reduce logging
});
//var NODE_ENV = 'development';
app.configure('development', function(){
app.use(express.errorHandler({
dumpExceptions: true,showStack: true
}));
});
app.get('/', function(req , res){
res.render('layout', {
title: 'My GeoLoc Map',
description: 'This is my first GeoLoc map'
}
);
});
app.io.route('ready', function(req){
req.io.respond({ status: 'ACKd' })
});
app.io.route('myLoc', function(req){
console.log('myLoc fired!');
//console.log(req);
var obj = {};
obj.name = req.name || 'username',
obj.data = req.data,
obj.id = req.io.socket.id,
obj.ua = req.headers['user-agent'],
obj.address = req.handshake.address.address;
req.io.broadcast('usrsLoc', obj);
var loc = new location({
name: obj.name,
id: obj.id,
ipAdd: obj.address,
latlng: { lat: obj.data.latlng.lat, lng: obj.data.latlng.lng },
userAgent: obj.ua,
timestamp: new Date
});
// Save to MongoDB
loc.save(function (err) {
if (err) return handleError(err);
// saved!
});
});
app.listen(app.get('port'), function(){
console.log('Express running at localhost on port' + app.get('port'));
});