Home > Software design >  Getting complete ip addrress in express, node.js
Getting complete ip addrress in express, node.js

Time:11-01

I was trying to get complete IP address of the client using express and node.js but what I am getting is ::1. I tried reading this How to get IP address in node.js express not could not find the solution

Here is my code.

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

const middleware = (req, _, next) => {
    console.log(req.ip)
    // Here I am getting ::1
    next()
}

app.get("/*",middleware, (req, res) => {
    
    res.send("Hello")
})

app.listen(3000,() => console.log("Server started"))

The above code is in the index.js file Here is my package.json file

{
  "name": "one",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  }
}

Can anyone please tell me how how can I get complete ip address?

CodePudding user response:

::1is the IPv6 equivalent of 127.0.0.1 - the loopback address. That is to be expected if you're connecting to your server from the same computer and using localhost as the hostname.

If you connect to your server from a different computer, you should see an actual client IP address.

  • Related