Develop a Node.js/express web app to know your ISP, Geolocation and more.......

ShakeebShakeeb
3 min read

Click here to have a demo ๐Ÿ”

This started with my curiosity to integrate the IP-logging of users visiting my portfolio website. (No way I'm collecting your data) ๐Ÿ˜’

So my curiosity asked me why not use a third service like https://ip-api.com/ to help me create an API that can provide a user ISP, Geolocation and most importantly IP. So after a tipsy-curvy lane of googling from here and there.

Started by moving to my vs code editor.

Created file --> server.js

moving on to my vs code terminal

git init -y

git install express

git install axios

git install request-ip

You can check the documentation of the request-ip module in node.js here https://www.npmjs.com/package/request-ip

Zooming in to server.js basic code structure was to require the modules of express, request-ip and axios.

Now, move on to writing the route handlers where the by using

app.get('/', async (req, res) => {}

const express = require('express');
const app = express();

const requestIP = require('request-ip');
const axios = require('axios');
app.listen(3000, () => {
  console.log('App listening on port 3000');
});

As you see it's an async function that will handle some API calls

We are using https://ip-api.com/ You may check the JSON format in the given website to add data as per your need.

requestIP.getClientIp(req);

This line uses the requestIP library (or module) to obtain the IP address of the client making the HTTP request. The getClientIp function is called with the req object, which represents the HTTP request being handled. It extracts the client's IP address and assigns it to the ipAddress variable.

We use try and catch for better handling of the incoming requests.

hence try block is where asynchronous operations are performed, and any errors that occur will be caught in the corresponding catch block.

const { data } = await axios.get(http://ip-api.com/json/${ipAddress}`);`;`)

This line makes an HTTP GET request to the "http://ip-api.com/json/" API, which is used for IP geolocation. It appends the ipAddress variable to the URL as a parameter, effectively requesting information about the IP address of the client.

axios.get is used to perform the HTTP GET request. The await keyword is used to wait for the response to be received before continuing execution.

The response from the API is destructured to extract the data property from it. This data object likely contains information about the IP address's geolocation.

app.get('/', async (req, res) => {

  const ipAddress = requestIP.getClientIp(req);

  try {
    const {data} = await axios.get(`http://ip-api.com/json/${ipAddress}`);

    const info = {
      ip: ipAddress,
      city: data.city,
      region: data.regionName,
      country: data.country,
      org: data.org,
      isp: data.isp
    };

    let html = `
      <h1>IP Address Info</h1>
      <table>
        <tr><td>IP</td><td>${info.ip}</td></tr>
        <tr><td>City</td><td>${info.city}</td></tr>
        <tr><td>Region</td><td>${info.region}</td></tr>
        <tr><td>Country</td><td>${info.country}</td></tr>
        <tr><td>ISP</td><td>${info.isp}</td></tr>
      </table>
    `;

    res.send(html);

  } catch (err) {
    console.error(err);
    res.status(500).send('Error getting IP info');
  }

});

So, in a nutshell, this code sets up a route handler that, when accessed, fetches geolocation information based on the client's IP address using the IP-API service and then structures that information into an object called info. This info object can be used later in the code, or you can send it as a response to the client's HTTP request.

Happy Coding!๐Ÿ‘ฝ

0
Subscribe to my newsletter

Read articles from Shakeeb directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shakeeb
Shakeeb