Created
September 26, 2018 11:09
-
-
Save JohannesHoppe/f8e9fffe651030dbd9da21f7ffa3bdb8 to your computer and use it in GitHub Desktop.
What is my ip?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const app = express(); | |
var port = process.env.port || 8080; | |
app.get('/', (req, res) => res.send('Hello stranger! <a href="/ip">Click here</a>')); | |
app.get('/ip', (req, res) => { | |
let ip = (req.headers['x-forwarded-for'] || '').split(',').pop() || | |
req.connection.remoteAddress || | |
req.socket.remoteAddress || | |
req.connection.socket.remoteAddress | |
// removes optional port | |
ip = ip.split(':')[0] | |
res.send(ip); | |
}); | |
app.listen(port, () => console.log(`App listening on port ${port}!`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment