Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase hosting/functions - how get real hostname (domain name)

I have multiple domains example.com and example.nl

I want to make redirect from example.nl to example.com/nl But I can't see real domain name, because I get us-central1-example-name.cloudfunctions.net in req.hostname

The function is:

const functions = require('firebase-functions')
const express = require('express')


const app = express()

app.get("*", (req, res) => {
  if(req.hostname == 'example.nl'){
    return res.status(302).redirect("https://example.com/nl");
  }

  return res.status(200).send('Hello');
})
exports.nuxtApp = functions.https.onRequest(app)

I couldn't find any instruction how to do it in firebase hosting documentation

UPD: I've found solution. (req.headers['x-forwarded-host'])

const functions = require('firebase-functions')
const express = require('express')


const app = express()

app.get("*", (req, res) => {
  if(req.headers['x-forwarded-host'] == 'example.nl'){
    return res.status(302).redirect("https://example.com/nl");
  }

  return res.status(200).send('Hello');
})
exports.nuxtApp = functions.https.onRequest(app)
like image 207
Oleksii.B Avatar asked Nov 07 '22 17:11

Oleksii.B


1 Answers

req.headers["x-forwarded-host"] - this worked for me

like image 106
Vikash Sharma Avatar answered Nov 16 '22 11:11

Vikash Sharma