当前位置:网站首页>nodejs获取客户端ip
nodejs获取客户端ip
2022-07-06 23:41:00 【samscat】
// 方法1
function getClientIp(req) {
var ipAddress;
var forwardedIpsStr = req.headers['X-Forwarded-For'];//判断是否有反向代理头信息
if (forwardedIpsStr) {
//如果有,则将头信息中第一个地址拿出,该地址就是真实的客户端IP;
var forwardedIps = forwardedIpsStr.split(',');
ipAddress = forwardedIps[0];
}
if (!ipAddress) {
//如果没有直接获取IP;
ipAddress = req.connection.remoteAddress;
}
return ipAddress;
};
// 方法2
function getClientIp2(req) {
return req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;
};
const http = require('http')
const server = http.createServer()
//获取客户端真实ip
function getClientIp(req) {
return req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;
};
server.on('request',(req, res) => {
const url = req.url;
const method = req.method;
const str = `您请求的地址:${
url},请求方法:${
method}`
// 设置请求头,解决中文乱码问题
res.setHeader('Content-Type','text/html;charset=utf-8')
res.write('ip', 'utf8', () => {
console.log('ippp', getClientIp(req))
})
// 发送数据给客户端和结束请求
res.end(str)
})
server.listen('81',() => {
console.log('start')
})
边栏推荐
- App clear data source code tracking
- EGR-20USCM接地故障继电器
- 阿里云的神龙架构是怎么工作的 | 科普图解
- 漏电继电器JD1-100
- 做自媒体,有哪些免费下载视频剪辑素材的网站?
- DFS, BFS and traversal search of Graphs
- 论文阅读【MM21 Pre-training for Video Understanding Challenge:Video Captioning with Pretraining Techniqu】
- Full link voltage test: the dispute between shadow database and shadow table
- [question] Compilation Principle
- 高压漏电继电器BLD-20
猜你喜欢
Use Zhiyun reader to translate statistical genetics books
Dj-zbs2 leakage relay
Digital innovation driven guide
《5》 Table
Unity让摄像机一直跟随在玩家后上方
漏电继电器LLJ-100FS
Phenomenon analysis when Autowired annotation is used for list
JVM(十九) -- 字节码与类的加载(四) -- 再谈类的加载器
DOM-节点对象+时间节点 综合案例
Safe landing practice of software supply chain under salesforce containerized ISV scenario
随机推荐
说一说MVCC多版本并发控制器?
Leetcode: maximum number of "balloons"
Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
LabVIEW is opening a new reference, indicating that the memory is full
app clear data源码追踪
K6el-100 leakage relay
照片选择器CollectionView
Scheduledexecutorservice timer
Window scheduled tasks
Use Zhiyun reader to translate statistical genetics books
Harmonyos fourth training
Mysql database learning (8) -- MySQL content supplement
论文阅读【Sensor-Augmented Egocentric-Video Captioning with Dynamic Modal Attention】
AOSP ~binder communication principle (I) - Overview
Sorry, I've learned a lesson
Two methods of thread synchronization
[论文阅读] A Multi-branch Hybrid Transformer Network for Corneal Endothelial Cell Segmentation
Tencent cloud database public cloud market ranks top 2!
Digital innovation driven guide
Leetcode 1189 maximum number of "balloons" [map] the leetcode road of heroding