/api/user/geoip/country (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.geoip.Country.get(req)Returns object
{
"continent": {
"code": "NA",
"geoname_id": 6255149,
"names": {
"de": "Nordamerika",
"en": "North America",
"es": "Norteamérica",
"fr": "Amérique du Nord",
"ja": "北アメリカ",
"pt-BR": "América do Norte",
"ru": "Северная Америка",
"zh-CN": "北美洲"
}
},
"country": {
"geoname_id": 6252001,
"iso_code": "US",
"names": {
"de": "USA",
"en": "United States",
"es": "Estados Unidos",
"fr": "États-Unis",
"ja": "アメリカ合衆国",
"pt-BR": "Estados Unidos",
"ru": "США",
"zh-CN": "美国"
}
},
"registered_country": {
"geoname_id": 6252001,
"iso_code": "US",
"names": {
"de": "USA",
"en": "United States",
"es": "Estados Unidos",
"fr": "États-Unis",
"ja": "アメリカ合衆国",
"pt-BR": "Estados Unidos",
"ru": "США",
"zh-CN": "美国"
}
}
}
Receives
API routes may receive parameters from the URL and POST supporting simple and multipart:
Field | Value | Required | Type |
---|---|---|---|
ip | string | required | URL |
Exceptions
These exceptions are thrown (NodeJS) or returned as JSON (HTTP) if you provide incorrect data or do not meet the requirements:
Exception | Circumstances |
---|---|
invalid-ip | missing querystring ip |
invalid querystring ip |
NodeJS source (view on github)
const maxmind = require('maxmind')
const path = require('path')
let db
module.exports = {
auth: false,
get: async (req) => {
if (!req.query || !req.query.ip) {
throw new Error('invalid-ip')
}
if (!db) {
db = await maxmind.open(path.join(__dirname, '../../../../GeoLite2-Country.mmdb'))
}
if (process.env.NODE_ENV !== 'production' && req.query.ip === '127.0.0.1') {
req.query.ip = '8.8.8.8'
}
const country = db.get(req.query.ip)
if (country === null) {
throw new Error('invalid-ip')
}
return country
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
describe('/api/user/geoip/country', () => {
describe('exceptions', () => {
describe('invalid-ip', () => {
it('missing querystring ip', async () => {
const req = TestHelper.createRequest('/api/user/geoip/country')
let errorMesage
try {
await req.get()
} catch (error) {
errorMesage = error.message
}
assert.strictEqual(errorMesage, 'invalid-ip')
})
it('invalid querystring ip', async () => {
const req = TestHelper.createRequest('/api/user/geoip/country?ip=invalid')
let errorMesage
try {
await req.get()
} catch (error) {
errorMesage = error.message
}
assert.strictEqual(errorMesage, 'invalid-ip')
})
})
})
describe('receives', () => {
it('required querystring ip', async () => {
const req = TestHelper.createRequest('/api/user/geoip/country?ip=8.8.8.8')
const country = await req.get()
assert.strictEqual(country.country.iso_code, 'US')
})
})
describe('returns', () => {
it('object', async () => {
const req = TestHelper.createRequest('/api/user/geoip/country?ip=8.8.8.8')
req.filename = __filename
req.saveResponse = true
const country = await req.get()
assert.strictEqual(country.country.iso_code, 'US')
})
})
})