简体   繁体   English

nuxt.js - 无法定义主机和端口

[英]nuxt.js - unable to define host and port

nuxt.js always defaults to localhost despite host being defined as frontend.gradez.loc in nuxt.config.js尽管主机在nuxt.config.js中定义为frontend.gradez.loc ,但 nuxt.js 始终默认为localhost

Contents of nuxt.config.js : nuxt.config.js的内容:

server: {
  host: 'frontend.gradez.loc',
  port: 3000
},

Contents of package.json : package.json的内容:

"config": {
  "nuxt": {
    "host": "frontend.gradez.loc",
    "port": "3000"
  }
}

nuxt launch script as dev : nuxt启动脚本为dev

"dev": "nuxt --hostname frontend.gradez.loc --port 3000",

For some odd reason when starting the development script it always defaults to: Listening on: http://localhost:3000/由于某些奇怪的原因,在启动开发脚本时它总是默认为: Listening on: http://localhost:3000/

I tried to do exactly the same on react and the only thing I had to do was create a .env file and inside it I added host=frontend.gradez.loc and it worked just like that.我尝试在 react 上做同样的事情,我唯一要做的就是创建一个.env文件,并在其中添加了host=frontend.gradez.loc ,它就像那样工作。

To create your server, under the hood Nuxt uses Node's http.createServer , then calls listen({ host, port }) on that server.为了创建您的服务器,Nuxt 在后台使用 Node 的http.createServer ,然后在该服务器上调用listen({ host, port })

So if on your local machine the hostname frontend.gradez.loc is mapped to 127.0.0.1 , which I assume is the case, then that server is running on the IP 127.0.0.1 .因此,如果在您的本地计算机上,主机名frontend.gradez.loc映射到127.0.0.1 ,我假设是这种情况,那么该服务器正在 IP 127.0.0.1上运行。

To create the url you see printed in Listening on... , Nuxt gets the IP of that underlying server, and maps it back to a hostname string.要创建 url 您会在Listening on...中看到打印的内容,Nuxt 获取该底层服务器的 IP,并将其映射回主机名字符串。 It statically maps the IP 127.0.0.1 to the string 'localhost' , so no matter what host you configure, if it maps to 127.0.0.1 then Nuxt will always map that to localhost in that url. It statically maps the IP 127.0.0.1 to the string 'localhost' , so no matter what host you configure, if it maps to 127.0.0.1 then Nuxt will always map that to localhost in that url. The code that does this is here .执行此操作的代码在这里

There's nothing incorrect per-se about reporting the server is running on localhost:3000 rather than frontend.gradez.loc:3000 .报告服务器在localhost:3000而不是frontend.gradez.loc:3000上运行本身并没有什么不正确的 It's literally true, in a networking sense, because both ultimately point to 127.0.0.1:3000 .从网络意义上来说,这确实是真的,因为两者最终都指向127.0.0.1:3000 So nothing is broken here from the perspective of the dev server, it's working as designed.所以从开发服务器的角度来看,这里没有任何问题,它按设计工作。

I'm not sure if you have anything automatically loading that url in the browser when you start the server - if so I can see how this is inconvenient from the perspective of other things in your workflow coupled to that hostname such as cookies, proxy servers etc - but if you manually type frontend.gradez.loc:3000 into your browser everything will just work.我不确定当您启动服务器时,您是否有任何东西在浏览器中自动加载 url - 如果是这样,我可以从与该主机名(例如 cookies、代理服务器)耦合的工作流中的其他事物的角度来看,这是多么不方便等等 - 但是如果你在浏览器中手动输入frontend.gradez.loc:3000一切都会正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM