A technical overview for administrators and developers
A user-location detection system automatically determines a visitor’s city, region, and country by querying an external geolocation API (e.g., ipinfo.io, ipapi.co). Administrators can view real-time or historical location data to tailor content, enforce regional policies, and monitor security.
Step | Request | Result |
---|---|---|
1 | GET /geo from client IP | Service determines IP if none provided |
2 | Service → DB of IP ranges | Finds matching location record |
3 | Returns JSON | {"ip":"203.0.113.9", "city":"Bengaluru", "country":"IN", ...} |
4 | Our backend stores location | Attaches to user session |
<!-- client.html extract -->
<script>
(async () => {
// Fetch location from backend proxy
const res = await fetch('/api/user/location');
const loc = await res.json();
console.log('Detected location:', loc);
})();
</script>
// backend/routes/location.js (Express)
import express from 'express';
import fetch from 'node-fetch';
const router = express.Router();
router.get('/location', async (req, res) => {
const ip = req.ip; // 1. Capture IP
const resp = await fetch(`https://ipapi.co/${ip}/json/`); // 2. Call API
const data = await resp.json();
// 3. Persist & return
await db.collection('userLocations').insertOne({
userId : req.session.userId,
ip : ip,
city : data.city,
region : data.region,
country: data.country_name,
ts : new Date()
});
res.json(data);
});
export default router;
An API-driven user-location detection layer empowers admins with actionable insights while enabling geo-based personalization and compliance controls. Pair robust security with clear consent mechanisms to deliver value without compromising user trust.