接下来JS如何根据IP地址重定向用户?
Next JS how to Redirect user based on the IP address?
我目前正在使用 express 和 app.get('/anyPath') 中的这个方法来确定客户端的 IP 地址。
var ip = req.headers['x-real-ip'] || req.connection.remoteAddress
if (ip.substr(0, 7) == "::ffff:") {
ip = ip.substr(7)
console.log(ip)
}
是否有不同的方法?我发现自己在所有 app.get() 调用中重复上述代码行以匹配页面的不同路径。
示例修改自 Express 文档 [中间件] https://expressjs.com/en/guide/using-middleware.html
app.use(function (req, res, next) {
if (req.method === 'get') {
var ip = req.headers['x-real-ip'] || req.connection.remoteAddress
if (ip.substr(0, 7) == "::ffff:") {
ip = ip.substr(7)
console.log(ip)
}
}
next()
})
我目前正在使用 express 和 app.get('/anyPath') 中的这个方法来确定客户端的 IP 地址。
var ip = req.headers['x-real-ip'] || req.connection.remoteAddress
if (ip.substr(0, 7) == "::ffff:") {
ip = ip.substr(7)
console.log(ip)
}
是否有不同的方法?我发现自己在所有 app.get() 调用中重复上述代码行以匹配页面的不同路径。
示例修改自 Express 文档 [中间件] https://expressjs.com/en/guide/using-middleware.html
app.use(function (req, res, next) {
if (req.method === 'get') {
var ip = req.headers['x-real-ip'] || req.connection.remoteAddress
if (ip.substr(0, 7) == "::ffff:") {
ip = ip.substr(7)
console.log(ip)
}
}
next()
})