Skip to content

Commit

Permalink
updates turfjs version to 4.3.1 and fixes Turfjs#2
Browse files Browse the repository at this point in the history
uses `freegeoip.net` instead of `www.mapbox.com/api/Location`
  • Loading branch information
tsamaya committed May 22, 2017
1 parent c89869e commit 1bdf321
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
20 changes: 10 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var express = require('express'),
app = express(),
hat = require('hat'),
ss = require('simple-statistics'),
turf = require('turf'),
turf = require('@turf/turf'),
request = require('request');

// Dynamically set the port that this application runs on so that Heroku
Expand Down Expand Up @@ -39,23 +39,23 @@ app.get('/first', function(req, res) {
app.get('/add/:key', function(req, res) {
var key = req.params.key;
var timing = +new Date() - checks[key] || 0;
var ip = (req.ip === '127.0.0.1') ? '199.188.195.78' : req.ip;
var ip = (req.ip === '127.0.0.1' || req.ip === '::1') ? '199.188.195.78' : req.ip;

// get the external client IP - this variable is filled because
// we enabled 'trust proxy' earlier.
if (req.ips && req.ips.length) ip = req.ips[0];

// Use Mapbox for figuring out user locations: in production use,
// Use freegeoip for figuring out user locations: in production use,
// you would want to use MaxMind or another geoip library directly.
request('https://www.mapbox.com/api/Location/' + ip, {
request('https://freegeoip.net/json/' + ip, {
json: true
}, function(err, place) {
// Check that Mapbox was able to locate the IP address at all
// before trying to add it to the map.
if (!err &&
place.body &&
place.body.lat !== undefined &&
place.body.lat !== 0) {
place.body.latitude !== undefined &&
place.body.latitude !== 0) {
var i;
var random_long_string = '';
for (i = 0; i < 100; i++) random_long_string += Math.random();
Expand All @@ -64,7 +64,7 @@ app.get('/add/:key', function(req, res) {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [place.body.lon, place.body.lat]
coordinates: [place.body.longitude, place.body.latitude]
},
properties: {
speed: timing
Expand All @@ -74,8 +74,8 @@ app.get('/add/:key', function(req, res) {
// avoid duplicating results that land in the same exact latitude
// and longitude position.
for (i = 0; i < points.features.length; i++) {
if (points.features[i].geometry.coordinates[0] == place.body.lon &&
points.features[i].geometry.coordinates[1] == place.body.lat) {
if (points.features[i].geometry.coordinates[0] === place.body.longitude &&
points.features[i].geometry.coordinates[1] === place.body.latitude) {
// avoid duplicates
return;
}
Expand All @@ -84,7 +84,7 @@ app.get('/add/:key', function(req, res) {
if (points.features.length > 1000) points.features.shift();
} else {
console.log(err, place);
res.end('done');
res.end('err');
}
});
});
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "turf-server-example",
"version": "1.0.0",
"version": "1.1.0",
"description": "an example of using turf in a server context",
"main": "index.js",
"scripts": {
Expand All @@ -21,7 +21,7 @@
"request": "^2.51.0",
"simple-statistics": "^0.9.0",
"transfer-rate": "^1.0.5",
"turf": "^1.3.2",
"@turf/turf": "^4.3.1",
"xhr": "^2.0.1"
},
"devDependencies": {
Expand Down

0 comments on commit 1bdf321

Please sign in to comment.