当前位置:网站首页>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')
})
边栏推荐
- Cve-2021-3156 vulnerability recurrence notes
- DOM node object + time node comprehensive case
- Leakage relay llj-100fs
- Paper reading [semantic tag enlarged xlnv model for video captioning]
- SAP webservice 测试出现404 Not found Service cannot be reached
- 论文阅读【Open-book Video Captioning with Retrieve-Copy-Generate Network】
- 《2022中国低/无代码市场研究及选型评估报告》发布
- 《2》 Label
- 【oracle】简单的日期时间的格式化与排序问题
- The navigation bar changes colors according to the route
猜你喜欢
随机推荐
Use, configuration and points for attention of network layer protocol (taking QoS as an example) when using OPNET for network simulation
async / await
《2022中国低/无代码市场研究及选型评估报告》发布
CentOS 7.9 installing Oracle 21C Adventures
SQL query: subtract the previous row from the next row and make corresponding calculations
[JS component] date display.
照片选择器CollectionView
拼多多新店如何获取免费流量,需要从哪些环节去优化,才能有效提升店内免费流量
分布式全局ID生成方案
Paper reading [semantic tag enlarged xlnv model for video captioning]
JVM (19) -- bytecode and class loading (4) -- talk about class loader again
Leakage relay llj-100fs
架构设计的五个核心要素
[binary tree] binary tree path finding
WEB架构设计过程
《2》 Label
CVE-2021-3156 漏洞复现笔记
English grammar_ Noun possessive
Pinduoduo product details interface, pinduoduo product basic information, pinduoduo product attribute interface
C#可空类型









