当前位置:网站首页>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')
})
边栏推荐
- 利用OPNET进行网络单播(一服务器多客户端)仿真的设计、配置及注意点
- Disk monitoring related commands
- ThinkPHP Association preload with
- 删除文件时提示‘源文件名长度大于系统支持的长度’无法删除解决办法
- [Oracle] simple date and time formatting and sorting problem
- 【js组件】date日期显示。
- Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
- Let f (x) = Σ x^n/n^2, prove that f (x) + F (1-x) + lnxln (1-x) = Σ 1/n^2
- Lombok插件
- 张平安:加快云上数字创新,共建产业智慧生态
猜你喜欢

Leetcode 1189 maximum number of "balloons" [map] the leetcode road of heroding

Getting started with DES encryption

人体传感器好不好用?怎么用?Aqara绿米、小米之间到底买哪个

JVM (19) -- bytecode and class loading (4) -- talk about class loader again

sql优化常用技巧及理解

Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting

JSP setting header information export to excel
![[PM products] what is cognitive load? How to adjust cognitive load reasonably?](/img/75/2277e0c413be561ec963b44679eb75.jpg)
[PM products] what is cognitive load? How to adjust cognitive load reasonably?

Window scheduled tasks

Use, configuration and points for attention of network layer protocol (taking QoS as an example) when using OPNET for network simulation
随机推荐
Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
磁盘监控相关命令
Y58. Chapter III kubernetes from entry to proficiency - continuous integration and deployment (Sany)
[QT] custom control loading
Mybaits之多表查询(联合查询、嵌套查询)
[JS component] custom select
App clear data source code tracking
Design, configuration and points for attention of network unicast (one server, multiple clients) simulation using OPNET
Creation and use of thread pool
AIDL 与Service
JHOK-ZBL1漏电继电器
How does redis implement multiple zones?
Batch size setting skills
Intelligent annotation scheme of entity recognition based on hugging Face Pre training model: generate doccano request JSON format
Flink SQL 实现读写redis,并动态生成Hset key
Autowired注解用于List时的现象解析
Two person game based on bevy game engine and FPGA
Knapsack problem (01 knapsack, complete knapsack, dynamic programming)
[JS component] date display.
Zero sequence aperture of leakage relay jolx-gs62 Φ one hundred