mirror of
https://github.com/italicsjenga/handlebars-ipinfo.git
synced 2024-11-24 05:21:29 +11:00
Getting info works as a promise!!!!!!
This commit is contained in:
parent
2c1956312a
commit
8e4ecd3773
62
index.ts
62
index.ts
|
@ -2,7 +2,7 @@ import express = require('express');
|
|||
import expresshb = require('express-handlebars');
|
||||
import http = require('http');
|
||||
import https = require('https');
|
||||
import promise = require('promise');
|
||||
import Promise = require('promise');
|
||||
|
||||
import { IPGeoJson } from './ip_geo';
|
||||
|
||||
|
@ -12,48 +12,38 @@ app.engine('handlebars', expresshb({ defaultLayout: 'main' }));
|
|||
app.set('view engine', 'handlebars');
|
||||
|
||||
app.get('/', function (req, res) {
|
||||
res.render('home');
|
||||
getIpInfo("::ffff:" + "119.17.156.106").country; //replace with req.ip
|
||||
getIpInfo("::ffff:" + "119.17.156.106").done(function (geoinfo: IPGeoJson) {
|
||||
res.render('home');
|
||||
}); //replace with req.ip
|
||||
});
|
||||
|
||||
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'
|
||||
function getIpInfo(ip: String): Promise.IThenable<{}> {
|
||||
return new Promise(function (resolve, reject) {
|
||||
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;
|
||||
var callback = function (response: http.IncomingMessage) {
|
||||
var str: string;
|
||||
|
||||
response.on('data', function (chunk) {
|
||||
str += chunk;
|
||||
});
|
||||
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);
|
||||
});
|
||||
}
|
||||
response.on('end', function () {
|
||||
console.log(str);
|
||||
resolve(<IPGeoJson>JSON.parse(str.slice(9)));
|
||||
});
|
||||
}
|
||||
|
||||
https.get(options, callback);
|
||||
|
||||
return ipinfo;
|
||||
https.get(options, callback);
|
||||
})
|
||||
}
|
|
@ -17,10 +17,12 @@
|
|||
"@types/express-handlebars": "0.0.29",
|
||||
"@types/handlebars": "^4.0.32",
|
||||
"@types/node": "^7.0.23",
|
||||
"@types/promise": "^7.1.30",
|
||||
"express": "^4.15.3",
|
||||
"express-handlebars": "^3.0.0",
|
||||
"handlebars": "^4.0.10",
|
||||
"nodemon": "^1.11.0",
|
||||
"promise": "^7.1.1",
|
||||
"ts-node": "^3.0.4",
|
||||
"typescript": "^2.3.4"
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue