/api/user/maxmind/country (GET)
await global.api.user.maxmind.Country.get(req) Located in MaxMind GeoIP module API
Returns object
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 querystring ip | |
invalid-ip | missing querystring ip |
Receives
API routes may receive parameters from the URL and POST supporting simple and multipart:
Field | Value | Required | Type |
---|---|---|---|
ip | string | required | URL |
NodeJS source (edit on github)
If you see a problem with the source submit a pull request 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')
}
db = db || maxmind.openSync(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 (edit on github)
Tests perform real HTTP requests against a running Dashboard server.
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
describe('/api/user/maxmind/country', () => {
describe('exceptions', () => {
describe('invalid-ip', () => {
it('missing querystring ip', async () => {
const req = TestHelper.createRequest('/api/user/maxmind/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/maxmind/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/maxmind/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/maxmind/country?ip=8.8.8.8')
req.filename = __filename
req.saveResponse = true
const country = await req.get()
assert.strictEqual(country.country.iso_code, 'US')
})
})
})