当前位置:网站首页>nodejs获取客户端ip
nodejs获取客户端ip
2022-07-06 23:41:00 【samscat】
// 方法1
function getClientIp(req) {
var ipAddress;
var forwardedIpsStr = req.headers['X-Forwarded-For'];//判断是否有反向代理头信息
if (forwardedIpsStr) {
//如果有,则将头信息中第一个地址拿出,该地址就是真实的客户端IP;
var forwardedIps = forwardedIpsStr.split(',');
ipAddress = forwardedIps[0];
}
if (!ipAddress) {
//如果没有直接获取IP;
ipAddress = req.connection.remoteAddress;
}
return ipAddress;
};
// 方法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()
//获取客户端真实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 = `您请求的地址:${
url},请求方法:${
method}`
// 设置请求头,解决中文乱码问题
res.setHeader('Content-Type','text/html;charset=utf-8')
res.write('ip', 'utf8', () => {
console.log('ippp', getClientIp(req))
})
// 发送数据给客户端和结束请求
res.end(str)
})
server.listen('81',() => {
console.log('start')
})
边栏推荐
- [JS component] date display.
- Dbsync adds support for mongodb and ES
- Intelligent annotation scheme of entity recognition based on hugging Face Pre training model: generate doccano request JSON format
- Use Zhiyun reader to translate statistical genetics books
- Window scheduled tasks
- Design, configuration and points for attention of network arbitrary source multicast (ASM) simulation using OPNET
- 创始人负债10亿,开课吧即将“下课”?
- 1.AVL树:左右旋-bite
- Knapsack problem unrelated to profit (depth first search)
- 高压漏电继电器BLD-20
猜你喜欢
QT simple layout box model with spring
论文阅读【MM21 Pre-training for Video Understanding Challenge:Video Captioning with Pretraining Techniqu】
Intelligent annotation scheme of entity recognition based on hugging Face Pre training model: generate doccano request JSON format
《5》 Table
Y58. Chapter III kubernetes from entry to proficiency - continuous integration and deployment (Sany)
The navigation bar changes colors according to the route
[binary tree] binary tree path finding
Leetcode (417) -- Pacific Atlantic current problem
DOM node object + time node comprehensive case
Is PMP really useful?
随机推荐
Vector and class copy constructors
数字化创新驱动指南
The navigation bar changes colors according to the route
Life experience of an update statement
在米家、欧瑞博、苹果HomeKit趋势下,智汀如何从中脱颖而出?
[optimal web page width and its implementation] [recommended collection "
JHOK-ZBG2漏电继电器
[binary tree] binary tree path finding
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
Use, configuration and points for attention of network layer protocol (taking QoS as an example) when using OPNET for network simulation
If you want to choose some departments to give priority to OKR, how should you choose pilot departments?
K6el-100 leakage relay
Use Zhiyun reader to translate statistical genetics books
Make web content editable
Summary of the mean value theorem of higher numbers
什么是依赖注入(DI)
JSP setting header information export to excel
JVM(十九) -- 字节码与类的加载(四) -- 再谈类的加载器
Lombok插件
Aidl and service