forked from DRTforGSA18F/marketapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
143 lines (123 loc) · 4 KB
/
index.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
var express = require('express'),
mongoskin = require('mongoskin'),
bodyParser = require('body-parser'),
logger = require('morgan');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(logger('dev'));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
var db = mongoskin.db('mongodb://drtuser:Go*[email protected]:31942/farmersmarkets', {
safe: true
});
app.set('port', (process.env.PORT || 5000));
app.use(function(req, res, next) {
req.db = db;
next();
});
app.get('/', function(req, res) {
var result = 'Hello World'
res.send(result);
});
/* Location API
Expect URL to be of format: markets/?loc=type&[zip, city, or state]=value. Examples:
markets/?loc=zip&zip=30135
markets/?loc=state&state=Georgia
markets/?loc=city&state=Georgia&city=Douglasville
markets/?loc=proximity&lat=x.xx&lng=x.xx&dist=x
*/
app.get('/markets/', function(req, res) {
var loctype = req.query.loc;
console.log("loctype: " + loctype);
//Expect URL to be: markets/?loc=zip&zip=30135
if (loctype=="zip") {
var zipcode = req.query.zip;
console.log("zip: " + zipcode);
req.db.collection('markets').find({"zip": zipcode}).toArray(function(err, items) {
if (err) {
console.log(err);
} else {
res.json(items);
}
});
}
//Expect URL to be: markets/?loc=state&state=Georgia
else if (loctype=="state") {
var state = req.query.state;
console.log("state: " + state);
req.db.collection('markets').find({"state": state}).toArray(function(err, items) {
if (err) {
console.log(err);
} else {
res.json(items);
}
});
}
//Expect URL to be: markets/?loc=city&state=Georgia&city=Douglasville
else if (loctype=="city") {
var state = req.query.state;
var city = req.query.city;
console.log("state: " + state);
console.log("city: " + city);
req.db.collection('markets').find({"state": state,"city": city}).toArray(function(err, items) {
if (err) {
console.log(err);
} else {
res.json(items);
}
});
}
//Expect URL to be: /markets/?loc=proximity&lat=36.841885&lng=-76.135361&dist=5
else if (loctype=="proximity") {
var latitude = parseFloat(req.query.lat);
var longitude = parseFloat(req.query.lng);
var distance = parseInt(req.query.dist);
var searchCriteria = [{
$geoNear: {
near: [longitude, latitude],
distanceField: 'distance',
maxDistance: ((distance / 1.25) / 3959),
spherical: true,
distanceMultiplier: (3959 * 1.25)
}
}];
db.collection('markets').aggregate(searchCriteria, function(err, items) {
if (err) {
res.send(500, {
'error': "Server Error - While trying to query db for proximity search." + err
});
} else {
res.json(items);
}
});
} else {
res.send("Error - Unhandled loctype: " + loctype +". Valid loctypes are zip, state, or city");
}
});
app.get('/market/:id', function(req, res) {
req.db.collection('markets').find({"_id": req.params.id}).toArray(function(err, items) {
if (err) {
console.log(err);
} else {
res.json(items);
}
})
});
app.get('/markets', function(req, res) {
req.db.collection('markets').find().toArray(function(err, items) {
if (err) {
console.log(err);
} else {
res.json(items);
}
});
});
app.listen(app.get('port'), function() {
console.log("Node app is running on port:" + app.get('port'))
})