mirror of
https://github.com/italicsjenga/handlebars-ipinfo.git
synced 2024-11-24 05:21:29 +11:00
getIpInfo function gets ip geoinfo
This commit is contained in:
parent
340829b6c9
commit
2c1956312a
48
index.ts
48
index.ts
|
@ -1,5 +1,10 @@
|
|||
import express = require('express');
|
||||
import expresshb = require('express-handlebars');
|
||||
import http = require('http');
|
||||
import https = require('https');
|
||||
import promise = require('promise');
|
||||
|
||||
import { IPGeoJson } from './ip_geo';
|
||||
|
||||
var app = express();
|
||||
|
||||
|
@ -8,6 +13,47 @@ app.set('view engine', 'handlebars');
|
|||
|
||||
app.get('/', function (req, res) {
|
||||
res.render('home');
|
||||
getIpInfo("::ffff:" + "119.17.156.106").country; //replace with req.ip
|
||||
});
|
||||
|
||||
app.listen(3000);
|
||||
app.listen(3000);
|
||||
|
||||
function getIpInfo(ip: String): IPGeoJson {
|
||||
var ipinfo: IPGeoJson = {
|
||||
ip: "",
|
||||
city: "",
|
||||
region: "",
|
||||
country: "",
|
||||
loc: "",
|
||||
postal: ""
|
||||
};
|
||||
|
||||
var options = {
|
||||
host: 'ipinfo.io',
|
||||
port: 443,
|
||||
path: '/' + ip + '/geo',
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
}
|
||||
console.log(options.path);
|
||||
|
||||
var callback = function (response: http.IncomingMessage) {
|
||||
var str: string;
|
||||
|
||||
response.on('data', function (chunk) {
|
||||
str += chunk;
|
||||
});
|
||||
|
||||
response.on('end', function () {
|
||||
console.log(str);
|
||||
ipinfo = <IPGeoJson>JSON.parse(str.slice(9));
|
||||
console.log(ipinfo.city);
|
||||
});
|
||||
}
|
||||
|
||||
https.get(options, callback);
|
||||
|
||||
return ipinfo;
|
||||
}
|
Loading…
Reference in a new issue