Menu

Show the cookie banner only to EU visitors: an Express middleware using IP geolocation
📰

Show the cookie banner only to EU visitors: an Express middleware using IP geolocation

DEV Community: express·Olly·4 days ago
#shzLeZ4P
#devtoexpress#devto#dev#const#rapidapi
Long article (831 words) — no AI summary yet.
Reading 0:00
15s threshold

Showing a consent banner to every visitor annoys people outside the EU, and hiding it from everyone is a compliance risk. A cheap middle path: look up the visitor's IP once, cache the answer, and show the banner only when the address is in an EU country.

The IP lookup endpoint returns an is_eu boolean, so there is no country list to maintain. Here is the real response for a German address (tested live):

{ "asn" : 60729 , "continent_code" : "EU" , "country" : "Germany" , "country_code" : "DE" , "ip" : "185.220.101.1" , "is_eu" : true , "is_global" : true , "is_private" : false , "organization" : "Stiftung Erneuerbare Freiheit" , "version" : 4 }

Enter fullscreen mode

Exit fullscreen mode

For 8.8.8.8 the same call gives "country_code": "US" and "is_eu": false .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.