How can I get the IP address of the source of a HTTP request to Google Cloud Functions (not Firebase)?

Viewed 934

I have a Google Cloud Function that is triggered by a HTML request. I need to get the source IP address. I've tried printing the whole request argument and it's not there.

Any ideas?

Note: this is NOT a duplicate of How to get client IP address in a Firebase cloud function?. The solution for Firebase Cloud Functions does not work on Google Cloud Functions.

1 Answers

Just use request.headers['x-forwarded-for']. Make sure you're not using any middleware in ExpressJS, because of this

So instead of doing this:

const app = express()
const cors = require('cors')({ origin: true })
app.use(cors)

app.use(require('body-parser').json())
app.use(express.json())
app.use(express.urlencoded({ extended: false }))

just use this:

const app = express()
Related