Firebase Hosting with dynamic cloud functions rewrites

前端 未结 3 1412
忘了有多久
忘了有多久 2020-12-05 02:57

I have an express.js based cloud functions app on firebase in a function named api. To use a custom domain, I\'m trying to use Firebase Hosting rewrites to rout

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-05 03:34

    Another option, if you want both cloudFunction and hosted URLS to work is to check if the URL is coming from the hosted URL.

    You'd use this function at any time you wanted the params.

    export const splitParams = (req: any): string[] => {
      let params = req.params[0];
      const vals: string[] = [];
    
      // If params starts with a '/' remove it
      params = params.startsWith('/')
        ? params.substr(1, params.length - 1)
        : params;
      params = params.split('/');
    
      // For hosted URLs the parameters need to be shifted
      if (
        req.headers['x-forwarded-host'] === 'myURL' 
      ) {
        params.shift();
      }
    
      for (const param of params) {
        if (param !== '') {
          vals.push(param);
        }
      }
    
      return vals;
    };
    

提交回复
热议问题