Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nuxt js, how can I get the host name of a web page at server side rendering

Tags:

vue.js

nuxt.js

At nuxtjs poroject, I have a configure variable of api environment like this:

[
{
  front_host: local-1.domain.com,
  backend_api: api-1.domain.com
},
{
  front_host: local-2.domain.com,
  backend_api: api-2.domain.com
},
]

I need get the right object element from the host name, for example, if the host name of my web page is "local-1.domain.name", then I shall get the object {front_host: local-1.domain.com, backend_api: api-2.domain.com}.

In nuxtjs, if the web page renders at the front end, then I can get host name by location.host, if at ssr(server side rendering), how can I get the host name?

like image 815
soarinblue Avatar asked May 02 '18 09:05

soarinblue


1 Answers

Like@divine said, there are several options you could choose from.

For example,

export default {
     async asyncData ({ req, res }) {
        if (process.server) {
         return { host: req.headers.host }
        }
    }

host will be your ssr host name

like image 160
Ash Avatar answered Oct 08 '22 01:10

Ash