forked from Paranormal-Map-tivity/Paranormal-Map-tivity
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
135 lines (123 loc) · 3.47 KB
/
server.js
File metadata and controls
135 lines (123 loc) · 3.47 KB
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
'use strict';
const express = require('express');
const cors = require('cors');
const superagent = require('superagent');
const app=express();
const TOKEN=process.env.TOKEN;
if(!TOKEN) throw new Error('TOKEN missing');
const PORT=process.env.PORT;
if(!PORT){throw new Error('PORT is a ghost');}
app.use(cors());
app.use(express.static('./public'));
/* use these to recieve posts if needed in the furture
app.use(express.json());
app.use(express.urlencoded({extended:true}));
*/
app.get('/',(req,res)=>res.sendFile('index.html'));
app.get('/api/spirit',(req, res)=>{
var s = req.query.south;
var n = req.query.north;
var w = req.query.west;
var e = req.query.east;
var query = `
SELECT row_index,location,longitude, latitude, description
FROM haunted_places_2
WHERE latitude BETWEEN ${s} AND ${n}
AND longitude BETWEEN ${w} AND ${e}
LIMIT 50
`;
superagent.post('https://api.data.world/v0/sql/timothyrenner/haunted-places')
.set('Authorization',`Bearer ${TOKEN}`)
.type('form')
.send({query})
.then((result)=>{
res.send(result.body);
})
.catch( err=>{
console.error(err);
res.sendStatus(500,err);
});
});
app.get('/api/alien', (req,res)=>{
var s = req.query.south;
var n = req.query.north;
var w = req.query.west;
var e = req.query.east;
var query = `
SELECT row_index,city, city_longitude, city_latitude, text
FROM nuforc_reports
WHERE city_latitude BETWEEN ${s} AND ${n}
AND city_longitude BETWEEN ${w} AND ${e}
LIMIT 50
`;
superagent.post('https://api.data.world/v0/sql/timothyrenner/ufo-sightings')
.set('Authorization', `Bearer ${TOKEN}`)
.type('form')
.send({query})
.then((result)=>{
res.send(result.body);
})
.catch( err=>{
console.error(err);
res.sendStatus(500,err);
});
});
app.get('/api/bigfoot', (req,res)=>{
var s = req.query.south;
var n = req.query.north;
var w = req.query.west;
var e = req.query.east;
var query = `
SELECT row_index,county, latitude, longitude, observed
FROM bfro_reports_geocoded
WHERE latitude BETWEEN ${s} AND ${n}
AND longitude BETWEEN ${w} AND ${e}
LIMIT 50
`;
superagent.post('https://api.data.world/v0/sql/timothyrenner/bfro-sightings-data')
.set('Authorization', `Bearer ${TOKEN}`)
.type('form')
.send({query})
.then((result)=>{
res.send(result.body);
})
.catch( err=>{
console.error(err);
res.sendStatus(500,err);
});
});
app.get('/api/:type/:index',(req,res)=>{
let apiUrl;
let table;
let id = parseInt(req.params.index);
if(req.params.type==='spirit'){
apiUrl='https://api.data.world/v0/sql/timothyrenner/haunted-places';
table='haunted_places_2';
}
else if(req.params.type==='alien'){
apiUrl='https://api.data.world/v0/sql/timothyrenner/ufo-sightings';
table='nuforc_reports';
}
else{
apiUrl='https://api.data.world/v0/sql/timothyrenner/bfro-sightings-data';
table='bfro_reports_geocoded';
}
superagent.post(apiUrl)
.set('Authorization', `Bearer ${TOKEN}`)
.type('form')
.send({query: `SELECT ${table}.*,row_index FROM ${table} WHERE row_index = '${id}' LIMIT 10`})
.then((result)=>{
if(result.body.length){
res.send(result.body[0]);
}
else{
res.sendStatus(404);
}
}, err=>{
res.sendStatus(500).send(err);
});
});
app.get('*', (req,res) =>{
res.sendFile('public/index.html',{root: '.'});
});
app.listen(PORT,()=>console.log('Listening on PORT:'+PORT));