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

how to get location with universal #19

Closed
theomathieubhvr opened this issue May 16, 2017 · 9 comments
Closed

how to get location with universal #19

theomathieubhvr opened this issue May 16, 2017 · 9 comments
Labels

Comments

@theomathieubhvr
Copy link
Contributor

Hey,

I'm submitting a ... (check one with "x")

[ ] bug report => check the README and search github for a similar issue or PR before submitting
[ ] support request => check the README and search github for a similar issue or PR before submitting
[x] feature request

What is the motivation / use case for changing the behavior?
I am creating a white label website on which the content shown is dependant from the url used by my users. I cannot use window.location as it is not available server side.

  • Angular version: 4.1.0

I would be pleased to help you implement this, but do you have any lead on how to do it ?

Thanks !!

PS: This topic seems interesting : http://stackoverflow.com/questions/43403649/how-to-get-full-base-url-including-server-port-and-protocol-in-angular-univer

@theomathieubhvr theomathieubhvr changed the title How to get location. How to get location with universal May 16, 2017
@fulls1z3 fulls1z3 changed the title How to get location with universal [question] how to get location with universal May 17, 2017
@fulls1z3
Copy link
Owner

Currently not, but I'll have a look at this probably in the weekend.

@Gbuomprisco
Copy link

@theomathieubhvr, I am not totally aware of your specific case, but can't (and shouldn't?) the router handle that?

@theomathieubhvr
Copy link
Contributor Author

@Gbuomprisco I'm trying to get the domain of the website I visit.

I tried to do this
console.warn((router as any).location._platformStrategy._platformLocation.location); with no luck server-side.

@fulls1z3
Copy link
Owner

Well, I can suggest you a simple workaround to retrieve that value using ngx-meta.

There's the applicationUrl parameter in config.json, which is passed to MetaStaticLoader on app.module. This value is rendered on the page as the og:url (<meta property="og:url" content="http://yourwebsite.com/current_route">).

You can always access the contents of og:url meta tag through the DOM, and then split the value using the slashes.

@fulls1z3 fulls1z3 self-assigned this May 22, 2017
@theomathieubhvr
Copy link
Contributor Author

Ok sorry but I checked and appplicationUrl parameter is very much static.

Maybe I haven't been clear, I will try to reexplain.

Assuming I have a news website with two categories, Politics and Economics. I want to offer http://politics.website.com and http://economics.website.com as urls that my user can go to.

These with ONE repository, yours. I want to know which user is on which website.

Is there any way to do this ?

Thanks :)

@theomathieubhvr
Copy link
Contributor Author

OK i managed to do it !

I can make a Pull request if needed (but I think this is too specific).

To sum up how I did, I created a custom OriginService for server (for browser I just set origin to window.location.origin).

import { Injectable, Inject } from '@angular/core';
import { Request } from 'express';
import { REQUEST } from '@nguniversal/express-engine/tokens';

@Injectable()
export class OriginService {
  public origin: string;

  constructor(@Inject(REQUEST) private request: Request) {
    this.origin = request.hostname;
  }
}

@mohitkadelongraph
Copy link

@theomathieubhvr I tried you method and I am able to get origin url but its giving an error on browser:

Uncaught TypeError: Cannot read property 'prototype' of undefined
    at Object.<anonymous> (request.js:31)
var req = Object.create(http.IncomingMessage.prototype)

@theomathieubhvr
Copy link
Contributor Author

Have you created a OriginService for the browser too ?

@fulls1z3 fulls1z3 removed their assignment Aug 10, 2017
@dr1ss
Copy link

dr1ss commented Aug 15, 2017

Any way to achieve this (get the express request object) with @ngx-universal/express-engine please ?

@fulls1z3 fulls1z3 changed the title [question] how to get location with universal how to get location with universal Sep 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants