
How to Geo-Block Countries in Express.js (Free IP Geolocation API)
Ever need to restrict your app to specific countries? Whether it's compliance (GDPR, sanctions), licensing, or fraud prevention — geo-blocking is a common requirement that most tutorials overcomplicate. Here's a clean, production-ready approach using Express.js and a free IP geolocation API. The Approach Extract the client's IP from the request Look up their country via IP geolocation Allow, block, or redirect based on rules No database downloads. No MaxMind account. No stale GeoIP files. Setup mkdir geo-blocking && cd geo-blocking npm init -y npm install express Get a free API key (200 credits, no credit card): Frostbyte API Gateway The Middleware // geo-block.js const API_KEY = process . env . FROSTBYTE_KEY ; const GEO_API = ' https://api.frostbyte.wiki ' ; // Cache lookups to avoid redundant API calls const geoCache = new Map (); const CACHE_TTL = 60 * 60 * 1000 ; // 1 hour async function getCountry ( ip ) { const cached = geoCache . get ( ip ); if ( cached && Date . now () - cached
Continue reading on Dev.to Tutorial
Opens in a new tab



