vis*_*zky 5 node.js express reactjs next.js
我目前正在 app.get('/anyPath') 中使用express和此方法来确定客户端的IP地址。
var ip = req.headers['x-real-ip'] || req.connection.remoteAddress
if (ip.substr(0, 7) == "::ffff:") {
ip = ip.substr(7)
console.log(ip)
}
Run Code Online (Sandbox Code Playgroud)
有不同的方法吗?我发现自己在所有 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()
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10247 次 |
| 最近记录: |