Node.js //傳入請求HttpRequest function getClientIp(req) { return req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddres
Node.js
//傳入請求HttpRequest function getClientIp(req) { return req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress; }
Express
//express框架則簡單許多 req.ip
意外收穫
發現這兩種方式獲取的ip地址都帶有首碼,不知為何,看了眼Express request.js源碼,沒找到不對的地方。所以用正則表達式處理了下
req.ip.match(/\d+\.\d+\.\d+\.\d+/)