当前位置:网站首页>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')
})
边栏推荐
- Mysql database learning (8) -- MySQL content supplement
- JVM (XX) -- performance monitoring and tuning (I) -- Overview
- When deleting a file, the prompt "the length of the source file name is greater than the length supported by the system" cannot be deleted. Solution
- Getting started with DES encryption
- Initial experience of annotation
- Leetcode 1189 maximum number of "balloons" [map] the leetcode road of heroding
- TabLayout修改自定义的Tab标题不生效问题
- Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
- Common skills and understanding of SQL optimization
- 《5》 Table
猜你喜欢
Differences and introduction of cluster, distributed and microservice
JVM(二十) -- 性能监控与调优(一) -- 概述
English grammar_ Noun possessive
什么是消息队列?
The 2022 China low / no code Market Research and model selection evaluation report was released
CentOS 7.9 installing Oracle 21C Adventures
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实战—入门到开发,浅析原子化服务》
Leakage relay jelr-250fg
How does mapbox switch markup languages?
随机推荐
Leakage relay llj-100fs
Mapbox Chinese map address
基于 hugging face 预训练模型的实体识别智能标注方案:生成doccano要求json格式
5. 数据访问 - EntityFramework集成
Leakage relay jelr-250fg
《4》 Form
SAP webservice 测试出现404 Not found Service cannot be reached
C#可空类型
Cve-2021-3156 vulnerability recurrence notes
Unity让摄像机一直跟随在玩家后上方
分布式全局ID生成方案
MySQL数据库学习(7) -- pymysql简单介绍
sql查询:将下一行减去上一行,并做相应的计算
1. AVL tree: left-right rotation -bite
Design, configuration and points for attention of network specified source multicast (SSM) simulation using OPNET
Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
Jhok-zbl1 leakage relay
三级菜单数据实现,实现嵌套三级菜单数据
微信小程序蓝牙连接硬件设备并进行通讯,小程序蓝牙因距离异常断开自动重连,js实现crc校验位
The navigation bar changes colors according to the route