Deno is a runtime designed for JavaScript, TypeScript, and WebAssembly languages. It’s built based on the V8 JavaScript engine and Rust programming language. Deno had been distributed as a single executable without any dependencies needed. User can just install and run without any worries. In addition, Deno supports the import of third party modules to be use in the script. For example, user can import a geolocation module to query the geolocation information of a particular IP address.
In this article, we are going to guide you on how to use IP2Location in Deno. You will learn how to use the IP2Location Deno module and the IP2Location BIN database to query the geolocation information of an IP address.
Installation
Before we start, you will need to download the latest release from https://github.com/ip2location/ip2location-deno/releases, unzip it and copy the mode.ts file and /src folder into your project root folder.
You will also need the IP2Location BIN database. You can download a free database from https://lite.ip2location.com or purchase a commercial database from https://www.ip2location.com/database/ip2location.
Steps
- Create a new js file called query_ip.js in your project root folder, copy the following piece of code into the file, and change the “YOUR_DATABASE_PATH” to the database you have obtained earlier:
import { IP2Location } from "./mod.ts"; let ip2location = new IP2Location(); ip2location.open(YOUR_DATABASE_PATH); let ip = '8.8.8.8'; let result = ip2location.getAll(ip); for (var key in result) { console.log(key + ": " + result[key]); }
- In your terminal, enter the following command to run the script:
deno run --allow-env --allow-read query_ip.js
. You will see the result showing up like the screenshot below:
Conclusion
Throughout this article, you have learn how to use the IP2Location Deno module to query the geolocation information of an IP address. You can then make use of the geolocation data. For example, you can identify where is the IP address located, and who is the owner of the IP address. This will help in various situation, such as serving correct content to your visitor in your website, or to validate the order received from your customer.