skip to Main Content

I am using a url rewriting functionality in my application(SparatcusV3.4).
I am calling my backend from node js to check a productcode exists or not
for that I need the current browser url entered by user in the address bar.

I am accessing the url using below code

const fullUrl =  req.protocol + '://' + req.get('host')

this is working fine on my local system but when deployed on any environment(by SAP)
this URL is coming as "127.0.0.1:4200" , what might be the problem here with environment ?

or what is the correct way to get the full browser url entered by the user ?

any help would be appreciated!!!

thanks in advance

2

Answers


  1. Chosen as BEST ANSWER
     const obj = {};
      const rc = request.headers.cookie;
      rc?.split(';')?.forEach((cookie: any) => {
         const parts = cookie?.split('=');
         obj[parts.shift().trim()] = decodeURI(parts?.join('='));
      });
    
      return obj;
    

    it can give list of all cookies in request object so with OBJ['RT'] can give the value and further splitting with '=' we cna get the exact request URL there from we can extract the host and the origin uding below code

    const cookieName = 'RT';
      const cookieObj = this.getCookieasObject(req);
      let fullURL = cookieObj[cookieName];
      if (fullURL) {
         fullURL = decodeURIComponent(JSON.parse(fullURL).split('=')[1]);
      }
    
     const url = new URL(fullURL);
      const baseUrl = `${url.protocol}//${url.hostname}`;
    

  2. Please refer to this part of Spartacus docs: https://sap.github.io/spartacus-docs/server-side-rendering-coding-guidelines/#getting-the-request-url-and-origin

    It suggests to use SERVER_REQUEST_URL and SERVER_REQUEST_ORIGIN injection tokens when using the setup that’s running SSR behind a proxy in order to resolve URLs.

    To use these optional tokens:

    • it is assumed you’re using Spartacus’ NgExpressEngineDecorator from @spartacus/setup/ssr in your server.ts file.
    • when injecting them, you should mark them as @Optional (per docs), as these are not available in CSR application.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search