Skip to content

Instantly share code, notes, and snippets.

@ChukwuEmekaAjah
Created October 31, 2018 13:14
Show Gist options
  • Save ChukwuEmekaAjah/1bfa036394fe860079b8ac735b7efbcb to your computer and use it in GitHub Desktop.
Save ChukwuEmekaAjah/1bfa036394fe860079b8ac735b7efbcb to your computer and use it in GitHub Desktop.
Extracting the hostname from a host string in a NodeJS request header.
function hostnameof(req) {
var host = req.headers.host
if (!host) {
return
}
var offset = host[0] === '['
? host.indexOf(']') + 1
: 0
var index = host.indexOf(':', offset)
return index !== -1
? host.substring(0, index)
: host
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment