在 Node.js Express 服务器中使用 req.socket.remoteAddress 获取用户 IP 地址时,如果显示 undefined,可能是代码中存在一个拼写错误。

代码中的 remoteAddresss 属性应该是 remoteAddress

以下是修正后的代码:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
    const ip = req.socket.remoteAddress;
    res.send('我的IP地址:' + ip);
})

app.listen(4000, () => {
    console.log('Server running at http://localhost:4000/');
});

修改后,IP 地址应该会正确显示。

Node.js Express 服务器获取用户 IP 地址时显示 undefined 的问题

原文地址: https://www.cveoy.top/t/topic/qhJk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录