-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (56 loc) · 1.63 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
require('dotenv').config();
const express = require('express');
const app = express();
const fetch = require('node-fetch');
//middleware
app.use(express.static(__dirname+'/public'));
app.use(express.json());
// view engine
app.set('view engine', 'ejs');
//PORT
const PORT = process.env.PORT || 4000;
//IPGeolocation API Key
const geoLocationAPIkey = process.env.geoLocationAPIkey;
//initialise empty variable from geoDataObj
let geoDataObj = "";
//initialise empty location variable from geoDataObj
let geoDataLocation = "";
//initialise empty ip variable from geoDataObj
let ip = "";
app.get('/',async (req,res) => {
try{
if(ip === ""){
const data = await fetch('https://api.ipify.org/?format=json')
const ipObj = await data.json();
ip = ipObj.ip;
}
const url = `https://geo.ipify.org/api/v1?apiKey=${geoLocationAPIkey}&ipAddress=${ip}`;
try{
const geoData = await fetch(url);
geoDataObj = await geoData.json();
const {"ip":ip, "location":location, "isp":isp} = geoDataObj;
geoDataLocation = location;
res.render('index',{geoDataObj});
}catch(err){
console.log(err);
}
}catch(err){
console.log(err);
res.json({err});
}
})
app.post('/postData',(req,res) =>{
try{
const data = req.body;
ip = data.inputVal;
res.redirect('back');
}
catch(err){
console.log(err);
res.json({err});
}
})
app.get('/geoDataLocation',(req,res) => {
res.json(geoDataLocation);
})
app.listen(PORT,() => console.log(`Listening to ${PORT}...`))