当前位置:网站首页>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')
})
边栏推荐
- 分布式全局ID生成方案
- DOM node object + time node comprehensive case
- Life experience of an update statement
- 论文阅读【Sensor-Augmented Egocentric-Video Captioning with Dynamic Modal Attention】
- Paper reading [open book video captioning with retrieve copy generate network]
- WEB架构设计过程
- Mybaits之多表查询(联合查询、嵌套查询)
- Flink SQL 实现读写redis,并动态生成Hset key
- 淘宝商品详情页API接口、淘宝商品列表API接口,淘宝商品销量API接口,淘宝APP详情API接口,淘宝详情API接口
- SAP webservice 测试出现404 Not found Service cannot be reached
猜你喜欢
How Alibaba cloud's DPCA architecture works | popular science diagram
English grammar_ Noun possessive
[PM products] what is cognitive load? How to adjust cognitive load reasonably?
JVM (XX) -- performance monitoring and tuning (I) -- Overview
JD commodity details page API interface, JD commodity sales API interface, JD commodity list API interface, JD app details API interface, JD details API interface, JD SKU information interface
《HarmonyOS实战—入门到开发,浅析原子化服务》
English语法_名词 - 所有格
分布式事务介绍
常用消息队列有哪些?
分布式全局ID生成方案
随机推荐
【Shell】清理nohup.out文件
常用消息队列有哪些?
WEB架构设计过程
"Multimodal" concept
English grammar_ Noun possessive
随机生成session_id
JVM(十九) -- 字节码与类的加载(四) -- 再谈类的加载器
Two person game based on bevy game engine and FPGA
CentOS 7.9 installing Oracle 21C Adventures
What is dependency injection (DI)
《HarmonyOS实战—入门到开发,浅析原子化服务》
batch size设置技巧
集群、分布式、微服務的區別和介紹
MySQL数据库学习(7) -- pymysql简单介绍
淘宝店铺发布API接口(新),淘宝oAuth2.0店铺商品API接口,淘宝商品发布API接口,淘宝商品上架API接口,一整套发布上架店铺接口对接分享
Most commonly used high number formula
Paper reading [open book video captioning with retrieve copy generate network]
Reading the paper [sensor enlarged egocentric video captioning with dynamic modal attention]
Web architecture design process
JVM(二十) -- 性能监控与调优(一) -- 概述