Created
October 31, 2018 13:14
-
-
Save ChukwuEmekaAjah/1bfa036394fe860079b8ac735b7efbcb to your computer and use it in GitHub Desktop.
Extracting the hostname from a host string in a NodeJS request header.
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
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