forked from bhoomikaPatil/to-do-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
81 lines (81 loc) · 2.44 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
81
// jshint esversion: 6
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const http = require("https");
const app = express();
// Declaring the ipAddress
let ipAddress = "";
// Init body-parser
app.use(bodyParser.urlencoded({
extended: true
}));
// setting up ejs
app.set('view engine', 'ejs');
// instructing to serve static files from "public"
app.use(express.static("public"));
// listen at port 3000
app.listen(3000, () => {
console.log("Death star ready at port: 3000");
});
// actions for get at home route
app.get("/", (req, res) => {
// making url for request to api server
const api_key = process.env.API_KEY;
const api_url = 'https://geo.ipify.org/api/v1?';
let url = "";
if (ipAddress === "") {
url += api_url + 'apiKey=' + api_key;
} else {
url += api_url + 'apiKey=' + api_key + '&ipAddress=' + ipAddress;
}
// requesting info from url
http.get(url, (response) => {
// logging status code
console.log(response.statusCode);
// retriving data
response.on("data", (data) => {
const ipAddressInfo = JSON.parse(data);
// console.log(ipAddressInfo);
const ip = ipAddressInfo.ip;
const city = ipAddressInfo.location.city;
const region = ipAddressInfo.location.region;
const country = ipAddressInfo.location.country;
const location = city + ", " + region + ", " + country;
const timezone = ipAddressInfo.location.timezone;
const isp = ipAddressInfo.isp;
const cordinates = {
lat: ipAddressInfo.location.lat,
lng: ipAddressInfo.location.lng
};
// init object to send to home.ejs
const ipInfo = {
ip: ip,
location: location,
timezone: timezone,
isp: isp,
cordinates: cordinates
};
// render home.ejs
res.render("home.ejs", {
ipInfo: ipInfo
});
});
});
});
// action for post at home route
app.post("/", (req, res) => {
// init ip Address
ipAddress = req.body.ipEntered;
if(ipAddress === ""){
res.render("failure");
}
else{
res.redirect("/");
}
});
// action for post >> failure
app.post("/failure", (req,res)=>{
res.redirect("/");
});