Skip to content

Instantly share code, notes, and snippets.

@iamwarning
Forked from katowulf/print_ip_and_headers.js
Created October 2, 2021 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamwarning/146a1254d474b4923bca028b45e05dde to your computer and use it in GitHub Desktop.
Save iamwarning/146a1254d474b4923bca028b45e05dde to your computer and use it in GitHub Desktop.
Print IP address and headers in Cloud Functions
const functions = require('firebase-functions');
const util = require('util');
exports.helloWorld = functions.https.onRequest((req, res) => {
// For Firebase Hosting URIs, use req.headers['fastly-client-ip']
// For callable functions, use rawRequest
// Some users have better success with req.headers['x-appengine-user-ip']
const ipAddress = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
const headers = JSON.stringify(req.headers, null, 2);
const message = util.format("<pre>Hello world!\n\nYour IP address: %s\n\nRequest headers: %s</pre>", ipAddress, headers);
res.send(message);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment