如何从控制器内确定给定请求的IP地址?例如(在快递中):
app.post('/get/ip/address', function (req, res) {
// need access to IP address here
})
如何从控制器内确定给定请求的IP地址?例如(在快递中):
app.post('/get/ip/address', function (req, res) {
// need access to IP address here
})
当前回答
如果使用express…
req.ip
我在查这个,然后我想,等等,我用的是快递。咄。
其他回答
Var ipaddress = (req。标题(“x-forwarded-for”)| | req.connection.remoteAddress | | req.socket.remoteAddress | | req.connection.socket.remoteAddress) .split (", ") [0];
在nodejs中简单获取远程ip:
var ip = req.header('x-forwarded-for') || req.connection.remoteAddress;
在shell中,只需curl https://api.ipify.org
所以,让我们观察一下,把它移植到node.js!
Curl是一个从网站获取数据的应用程序,我们传递网站“https://api.ipify.org”作为参数。我们可以使用节点取回来替换curl!
我们从网站上获得的数据是我们的IP地址,这是一种获取你的IP地址的东西。
总结一下:
const fetch = require('node-fetch');
fetch('https://api.ipify.org')
.then(response => {/* whatever */})
.catch(err => {/* whatever */})
对于我使用kubernetes ingress (NGINX):
req.headers['x-original-forwarded-for']
在Node.js中非常有效
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
var ip = req.ip
console.log(ip);
res.send('Hello World!')
})
// Run as nodejs ip.js
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})