Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get protocol in server-side rendering #2469

Closed
hank7444 opened this issue Jul 5, 2017 · 6 comments
Closed

Can't get protocol in server-side rendering #2469

hank7444 opened this issue Jul 5, 2017 · 6 comments

Comments

@hank7444
Copy link

hank7444 commented Jul 5, 2017

Hi All!

My question is i need to get origin dynamically,
I try req.headers and I got:

image

Yes!, I got host, but I also need the protocol, which needs to concat with host to create new api path. I browsed All req properties but I got nothing. Have any good idea? thx a lot!

@quentin-sommer
Copy link
Contributor

I think next use the express request so you should find it here: http://expressjs.com/fr/api.html#req.protocol

@timneutkens
Copy link
Member

timneutkens commented Jul 5, 2017

@quentin-sommer we don't use express. Just the plain http server build into node.js.

@quentin-sommer
Copy link
Contributor

Well that's my bad!

@quentin-sommer
Copy link
Contributor

quentin-sommer commented Jul 5, 2017

@hank7444 I dug in the req object, it has _parsedOriginalUrl and _parsedUrl fields which are both of type Url. The structure is as follows:

{
     protocol: null,
     slashes: null,
     auth: null,
     host: null,
     port: null,
     hostname: null,
     hash: null,
     search: null,
     query: null,
     pathname: '/page',
     path: '/page',
     href: '/page',
     _raw: '/page'
 }

I tested it only on localhost and as you see most of the fields are blank... I don't know if it's because I use a wrapper Expressjs server that calls next or if it's because of the localhost. Maybe you'll get what you want here. Be careful tho since _ prefixed fields are usually meant for private use

@timneutkens
Copy link
Member

It's safe to assume it's always http and the SSL termination has to be done by a reverse proxy like Nginx. These set the X-Forwarded-Proto header which you could use.

@franciscopuyo
Copy link

I managed to do that with following line
const protocol = req.headers.referer.split('://')[0]

It looks dirty anyway

@lock lock bot locked as resolved and limited conversation to collaborators Jan 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants