[英]nodejs to get client IP Address inside setinterval function
I'm working with nodejs
to get client IP Address.我正在使用
nodejs
获取客户端 IP 地址。 I configure the function inside setinterval
function with below code:我使用以下代码在
setinterval
function 中配置 function:
var countdown2 = setInterval(function(){
async function baked(req, res, id){
var getIP = req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;
var IP = getIP.slice(7);
console.log(IP);
}
baked();
}, 1000);
When I tried to run the code, it show me error like this:当我尝试运行代码时,它显示如下错误:
(node:16432) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'headers' of undefined
Any way how to make it works?任何方式如何使它工作?
Take a look at the request-ip
package.看一下
request-ip
package。
const express = require('express');
const app = express();
const { mw } = require('request-ip');
app.use(mw());
app.get('/', (req, res, next) => {
setInterval(() => console.log(req.clientIp), 1000);
});
app.listen(8000, () => console.log(`listening on 8000`))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.