-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
81 lines (68 loc) · 1.89 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
var express = require('express');
var app = express();
var mongoose = require('mongoose');
var fs = require('fs');
require('date-utils');
var SERVERPATH = '/opt/flick';
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
mongoose.connect('mongodb://10.100.0.5/flick');
/* スキーマ定義 */
mongoose.model('card', new mongoose.Schema({
userid: String,
time: Number,
location:{
type: [Number],
index: '2d'
},
data: String
}));
Card = mongoose.model('card');
/* フリックを送信する */
app.post('/flick', function(req, res, next) {
console.log("Flick request:" + req.body.jsondata);
var card = req.body.jsondata;
card.time = (new Date()).getTime();
Card.create(card, function (err, data) {
if (err) throw new Error(err);
res.send('{ result: "ok" }');
});
});
/* フリックを取得する */
app.get('/pull', function(req, res, next){
// 受け取れる範囲の距離をメートル単位で指定
var maxDistance = 100;
// 受け取れる範囲の時間を秒単位で指定
var time = 15;
maxDistance /= (6371*1000);
var time_from = (new Date()).getTime()-(1000*time);
console.log("Pull request by:" + req.param('userid') + ", location:[" + req.param('lng') + ", " + req.param('lat') + "].");
Card
.find({
location : {
$near : [req.param('lat'), req.param('lng')],
$maxDistance : maxDistance
},
time : {
$gte : time_from
}
})
.sort( 'time' )
.exec( function(err, doc){
res.send(doc);
});
});
/* デバッグ用:全てのフリックを取得する */
app.get('/getAllFlick', function(req, res, next){
Card.find({}, function(err, doc){
res.send(doc);
});
});
/* デバッグ用:全てのフリックを削除する */
app.get('/removeAllFlick', function(req, res, next){
Card.remove({}, function(err, doc){
res.send("{'result':'ok'}");
});
});
app.listen(3000);