从Nuxt中的请求获取当前域名 [英] Get current domain name from request in Nuxt

查看:430
本文介绍了从Nuxt中的请求获取当前域名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从服务器端的请求中获取当前域名?

How can I get the current domain name from a request on server side?

我的基于Nuxt的网站可以从不同的域访问.我想从用户访问网站的地方获取域名.我该怎么办?

My Nuxt based website is reachable from different domains. I would like to get the domain name from where the user has accessed the website. How can I do this?

我试图为此目的编写一个中间件,但它始终显示localhost:3000

I tried to write a middleware for that purpose but it always displays localhost:3000

export default function({ store, req }) {
  if (process.server) store.commit('hostname', req.headers.host)
}

您知道如何解决此问题吗?

Any idea how to fix this issue?

推荐答案

我可以通过以下方式获取在服务器上声明该功能的域名:

I'm able to get the domain name declaring the function on the server by this way:

export default function (req, res, next) {
    console.log(req.headers.host)
}

如果您需要将数据提交到存储,可以将数据保存在环境中并在客户端上进行恢复.

If you need to commit the data to the store, saving data in the environment and recover it on the client side could be a solution.

这篇关于从Nuxt中的请求获取当前域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆