当前位置:网站首页>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')
})
边栏推荐
- How Alibaba cloud's DPCA architecture works | popular science diagram
- Scheduledexecutorservice timer
- 《4》 Form
- JVM (19) -- bytecode and class loading (4) -- talk about class loader again
- 删除文件时提示‘源文件名长度大于系统支持的长度’无法删除解决办法
- Window scheduled tasks
- 痛心啊 收到教训了
- Is the human body sensor easy to use? How to use it? Which do you buy between aqara green rice and Xiaomi
- Dj-zbs2 leakage relay
- DOM-节点对象+时间节点 综合案例
猜你喜欢

Initial experience of annotation

The year of the tiger is coming. Come and make a wish. I heard that the wish will come true

Leakage relay llj-100fs

漏电继电器JOLX-GS62零序孔径Φ100

10 distributed databases that take you to the galaxy

《4》 Form

Digital innovation driven guide

How Alibaba cloud's DPCA architecture works | popular science diagram

Getting started with DES encryption
![[question] Compilation Principle](/img/ce/71f8409ba2cebd497bed0210290895.png)
[question] Compilation Principle
随机推荐
Vector and class copy constructors
1.AVL树:左右旋-bite
[QT] custom control loading
JHOK-ZBL1漏电继电器
删除文件时提示‘源文件名长度大于系统支持的长度’无法删除解决办法
Longest common subsequence (LCS) (dynamic programming, recursive)
设f(x)=∑x^n/n^2,证明f(x)+f(1-x)+lnxln(1-x)=∑1/n^2
Leetcode (46) - Full Permutation
How digitalization affects workflow automation
一条 update 语句的生命经历
基于 hugging face 预训练模型的实体识别智能标注方案:生成doccano要求json格式
阿里云的神龙架构是怎么工作的 | 科普图解
Design, configuration and points for attention of network specified source multicast (SSM) simulation using OPNET
Tablayout modification of customized tab title does not take effect
JHOK-ZBG2漏电继电器
The year of the tiger is coming. Come and make a wish. I heard that the wish will come true
ssm框架的简单案例
App clear data source code tracking
Complete code of C language neural network and its meaning
[JS component] date display.