当前位置:网站首页>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')
})
边栏推荐
- Initial experience of annotation
- Tablayout modification of customized tab title does not take effect
- DOM node object + time node comprehensive case
- JSP setting header information export to excel
- Two methods of thread synchronization
- Torch optimizer small parsing
- [question] Compilation Principle
- 1. AVL tree: left-right rotation -bite
- MySQL数据库学习(8) -- mysql 内容补充
- Leetcode (417) -- Pacific Atlantic current problem
猜你喜欢

Annotation初体验

Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem

Window scheduled tasks

利用OPNET进行网络单播(一服务器多客户端)仿真的设计、配置及注意点
![[PM products] what is cognitive load? How to adjust cognitive load reasonably?](/img/75/2277e0c413be561ec963b44679eb75.jpg)
[PM products] what is cognitive load? How to adjust cognitive load reasonably?

What changes will PMP certification bring?

Longest palindrome substring (dynamic programming)
![[JS component] date display.](/img/26/9bfc752c8c9a933a8e33b59e0488a2.jpg)
[JS component] date display.

Y58. Chapter III kubernetes from entry to proficiency - continuous integration and deployment (Sany)

JVM (19) -- bytecode and class loading (4) -- talk about class loader again
随机推荐
高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
利用OPNET进行网络单播(一服务器多客户端)仿真的设计、配置及注意点
Preliminary practice of niuke.com (9)
AIDL 与Service
A cool "ghost" console tool
ThinkPHP Association preload with
什么是依赖注入(DI)
How Alibaba cloud's DPCA architecture works | popular science diagram
Make web content editable
What changes will PMP certification bring?
Creation and use of thread pool
The year of the tiger is coming. Come and make a wish. I heard that the wish will come true
做自媒体视频剪辑,专业的人会怎么寻找背景音乐素材?
Annotation初体验
1. AVL tree: left-right rotation -bite
Under the trend of Micah, orebo and apple homekit, how does zhiting stand out?
照片选择器CollectionView
EGR-20USCM接地故障继电器
Wonderful express | Tencent cloud database June issue
Longest palindrome substring (dynamic programming)