Search code examples
spartacus-storefront

how to get browser full url of the page in nodejs express in environment, seems nginx issue?


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


Solution

  •  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}`;