我試圖使用 express 和 node.js 獲取客戶端的完整 IP 地址,但我得到的是 ::1。我嘗試閱讀這篇How to get IP address in node.js express not could not find the solution
這是我的代碼。
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"))
上面的代碼在 index.js 檔案中 這是我的 package.json 檔案
{
"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"
}
}
誰能告訴我如何獲得完整的IP地址?
uj5u.com熱心網友回復:
::1是 IPv6 等價物127.0.0.1- 環回地址。如果您從同一臺計算機連接到您的服務器并localhost用作主機名,那么這是可以預料的。
如果您從另一臺計算機連接到您的服務器,您應該會看到一個實際的客戶端 IP 地址。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/343601.html
上一篇:帶有app.use((err,req,res,next)=>{})和app.use("*",(err,req,res,next)=>{})的Express404錯誤處理
