Files
d1kt/server/routes/visitor.ts

24 lines
746 B
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import express from 'express';
const router = express.Router();
// 获取访问者IP地址
router.get('/ip', (req, res) => {
try {
// 获取X-Forwarded-For头信息如果通过代理
const forwardedIp = req.headers['x-forwarded-for'];
// 如果存在X-Forwarded-For则使用第一个IP最接近用户的代理
// 否则使用直接连接的IP
const ip = forwardedIp
? (typeof forwardedIp === 'string' ? forwardedIp.split(',')[0].trim() : forwardedIp[0])
: req.ip || req.connection.remoteAddress;
res.json({ ip });
} catch (error) {
console.error('获取IP地址错误:', error);
res.status(500).json({ message: '获取IP地址失败' });
}
});
export default router;