Skip to content

Instantly share code, notes, and snippets.

@srfrnk
Created August 16, 2014 12:23
Show Gist options
  • Save srfrnk/5a3a9f5ddd61c71572fd to your computer and use it in GitHub Desktop.
Save srfrnk/5a3a9f5ddd61c71572fd to your computer and use it in GitHub Desktop.
require("requirejs").define("models/serverUtils", [], function () {
return {
getClientIp: function (req) {
// snippet taken from http://catapulty.tumblr.com/post/8303749793/heroku-and-node-js-how-to-get-the-client-ip-address
var ipAddress;
// The request may be forwarded from local web server.
var forwardedIpsStr = req.header('x-forwarded-for');
if (forwardedIpsStr) {
// 'x-forwarded-for' header may return multiple IP addresses in
// the format: "client IP, proxy 1 IP, proxy 2 IP" so take the
// the first one
var forwardedIps = forwardedIpsStr.split(',');
ipAddress = forwardedIps[0];
}
if (!ipAddress) {
// If request was not forwarded
ipAddress = req.connection.remoteAddress;
}
return ipAddress;
},
urlTitle: function (title) {
return title.toLowerCase().replace(/[^a-zA-Z0-9]+/g, '-');
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment