You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
find Approximate location of a user based on IPadress
for frontend
// Make an API request to an IP geolocation servicefetch('https://ipapi.co/json/').then(response=>response.json()).then(data=>{// Extract the approximate location details from the responseconst{ city, region, country_name }=data;// Use the location details as neededconsole.log(`Approximate location: ${city}, ${region}, ${country_name}`);}).catch(error=>{console.error('Error retrieving location:',error);});
for backend
constgeoip=require('geoip-lite');// Get IP address of the userconstipAddress=req.headers['x-forwarded-for']||req.connection.remoteAddress;// Retrieve the approximate location based on the IP addressconstlocation=geoip.lookup(ipAddress);if(location){const{ city, region, country }=location;console.log(`Approximate location: ${city}, ${region}, ${country}`);}else{console.log('Location not found.');}