当前位置:网站首页>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')
})
边栏推荐
- What are the common message queues?
- Two person game based on bevy game engine and FPGA
- 爬虫练习题(三)
- 集群、分布式、微服务的区别和介绍
- [binary tree] binary tree path finding
- 论文阅读【Semantic Tag Augmented XlanV Model for Video Captioning】
- [PM products] what is cognitive load? How to adjust cognitive load reasonably?
- What is dependency injection (DI)
- 【js组件】date日期显示。
- 京东商品详情页API接口、京东商品销量API接口、京东商品列表API接口、京东APP详情API接口、京东详情API接口,京东SKU信息接口
猜你喜欢

How digitalization affects workflow automation

分布式事务解决方案之TCC

JVM (XX) -- performance monitoring and tuning (I) -- Overview

Mysql database learning (8) -- MySQL content supplement

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

集群、分布式、微服务的区别和介绍

5. 数据访问 - EntityFramework集成

集群、分布式、微服務的區別和介紹

Record a pressure measurement experience summary

拼多多商品详情接口、拼多多商品基本信息、拼多多商品属性接口
随机推荐
SAP ABAP BDC(批量数据通信)-018
淘宝店铺发布API接口(新),淘宝oAuth2.0店铺商品API接口,淘宝商品发布API接口,淘宝商品上架API接口,一整套发布上架店铺接口对接分享
常用消息队列有哪些?
淘寶商品詳情頁API接口、淘寶商品列錶API接口,淘寶商品銷量API接口,淘寶APP詳情API接口,淘寶詳情API接口
Unity keeps the camera behind and above the player
async / await
JVM (19) -- bytecode and class loading (4) -- talk about class loader again
Design, configuration and points for attention of network unicast (one server, multiple clients) simulation using OPNET
基于 hugging face 预训练模型的实体识别智能标注方案:生成doccano要求json格式
Preliminary practice of niuke.com (9)
Common skills and understanding of SQL optimization
分布式事务介绍
Mysql database learning (8) -- MySQL content supplement
什么是消息队列?
得物客服一站式工作台卡顿优化之路
The year of the tiger is coming. Come and make a wish. I heard that the wish will come true
Jhok-zbg2 leakage relay
JVM(十九) -- 字节码与类的加载(四) -- 再谈类的加载器
Leakage relay jd1-100
Sorry, I've learned a lesson