当前位置:网站首页>Nodejs get client IP
Nodejs get client IP
2022-07-07 05:41:00 【samscat】
// Method 1
function getClientIp(req) {
var ipAddress;
var forwardedIpsStr = req.headers['X-Forwarded-For'];// Determine whether there is reverse proxy header information
if (forwardedIpsStr) {
// If there is , Take out the first address in the header information , The address is the real client IP;
var forwardedIps = forwardedIpsStr.split(',');
ipAddress = forwardedIps[0];
}
if (!ipAddress) {
// If there is no direct access IP;
ipAddress = req.connection.remoteAddress;
}
return ipAddress;
};
// Method 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()
// Get client reality 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 = ` The address you requested :${
url}, Request method :${
method}`
// Set request header , Solve the problem of Chinese garbled code
res.setHeader('Content-Type','text/html;charset=utf-8')
res.write('ip', 'utf8', () => {
console.log('ippp', getClientIp(req))
})
// Send data to the client and end the request
res.end(str)
})
server.listen('81',() => {
console.log('start')
})
边栏推荐
- 拼多多商品详情接口、拼多多商品基本信息、拼多多商品属性接口
- 照片选择器CollectionView
- 三级菜单数据实现,实现嵌套三级菜单数据
- Zero sequence aperture of leakage relay jolx-gs62 Φ one hundred
- Leakage relay llj-100fs
- Pinduoduo product details interface, pinduoduo product basic information, pinduoduo product attribute interface
- Lombok plug-in
- zabbix_get测试数据库失败
- Differences and introduction of cluster, distributed and microservice
- 不同网段之间实现GDB远程调试功能
猜你喜欢
Flink SQL 实现读写redis,并动态生成Hset key
[PM products] what is cognitive load? How to adjust cognitive load reasonably?
照片选择器CollectionView
Unity keeps the camera behind and above the player
JVM (XX) -- performance monitoring and tuning (I) -- Overview
[paper reading] semi supervised left atrium segmentation with mutual consistency training
得物客服一站式工作台卡顿优化之路
ssm框架的简单案例
Five core elements of architecture design
C nullable type
随机推荐
MySQL-CentOS7通过YUM安装MySQL
WEB架构设计过程
AI face editor makes Lena smile
Most commonly used high number formula
什么是消息队列?
Common skills and understanding of SQL optimization
Life experience of an update statement
Flinksql 读写pgsql
win配置pm2开机自启node项目
nodejs获取客户端ip
How to get free traffic in pinduoduo new store and what links need to be optimized in order to effectively improve the free traffic in the store
Leetcode: maximum number of "balloons"
京东商品详情页API接口、京东商品销量API接口、京东商品列表API接口、京东APP详情API接口、京东详情API接口,京东SKU信息接口
Preliminary practice of niuke.com (9)
《5》 Table
Dj-zbs2 leakage relay
Jhok-zbg2 leakage relay
Reading the paper [sensor enlarged egocentric video captioning with dynamic modal attention]
Taobao commodity details page API interface, Taobao commodity list API interface, Taobao commodity sales API interface, Taobao app details API interface, Taobao details API interface
The navigation bar changes colors according to the route