当前位置:网站首页>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')
})
边栏推荐
- Two person game based on bevy game engine and FPGA
- 分布式全局ID生成方案
- Aidl and service
- 4. Object mapping Mapster
- How does mapbox switch markup languages?
- Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
- "Multimodal" concept
- Digital innovation driven guide
- Use Zhiyun reader to translate statistical genetics books
- Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
猜你喜欢
随机推荐
得物客服一站式工作台卡顿优化之路
4. 对象映射 - Mapping.Mapster
Flinksql 读写pgsql
基于 hugging face 预训练模型的实体识别智能标注方案:生成doccano要求json格式
《4》 Form
[binary tree] binary tree path finding
Zero sequence aperture of leakage relay jolx-gs62 Φ one hundred
Design, configuration and points for attention of network unicast (one server, multiple clients) simulation using OPNET
Jhok-zbg2 leakage relay
[reading of the paper] a multi branch hybrid transformer network for channel terminal cell segmentation
Simple case of SSM framework
基于NCF的多模块协同实例
sql查询:将下一行减去上一行,并做相应的计算
JVM(十九) -- 字节码与类的加载(四) -- 再谈类的加载器
async / await
Leetcode 1189 maximum number of "balloons" [map] the leetcode road of heroding
Digital innovation driven guide
[paper reading] semi supervised left atrium segmentation with mutual consistency training
MySQL-CentOS7通过YUM安装MySQL
JSP setting header information export to excel









