简体   繁体   English

next.js:通过next.config.js配置主机名和端口的方式

[英]next.js: way to configure hostname and port through next.config.js

i am using next.js 9.5.x and I am looking for a way to configure hostname and port through next.config.js.我正在使用 next.js 9.5.x,我正在寻找一种通过 next.config.js 配置主机名和端口的方法。 I already searched the documentation about this, but I did not find a answer for this topic.我已经搜索了有关此的文档,但没有找到该主题的答案。

I read already a series of.env files in the next.config.js file for setting up serval things and passing some of the environment variables to the application code itself.我已经阅读了 next.config.js 文件中的一系列 .env 文件,用于设置服务并将一些环境变量传递给应用程序代码本身。 I got already some environment variables specifying the hostname and port and would like to reuse them to setup the hostname and port for the server / development-server.我已经得到了一些指定主机名和端口的环境变量,并且想重用它们来设置服务器/开发服务器的主机名和端口。

The only two solutions I found, for changing these two parameters, are the command line settings of next, or having a custom server.js.我找到的唯一两个解决方案,用于更改这两个参数,是 next 的命令行设置,或者自定义 server.js。 At the moment I am using a custom server.js and inside I read all.env files again, just for having the port and hostname setup.目前我正在使用自定义 server.js 并在内部再次读取 all.env 文件,只是为了设置端口和主机名。

I want to get rid of the server.js, since I believe that there must be a way for configuring this through the next.config.js file, I have anyway, and where everything is already in place.我想摆脱 server.js,因为我相信一定有一种方法可以通过 next.config.js 文件来配置它,无论如何我都有,而且一切都已经到位了。

Thank you for some information about this.感谢您提供有关此的一些信息。

To change port:更改端口:

In package.josn , => scripts => add "dev": "next dev -p 5500 "package.josn ,=> 脚本 => 添加“dev”:“next dev -p 5500

"scripts": {
    "dev": "next dev -p 5500",
    "build": "next build",
    "start": "next start"
  }

在此处输入图片说明

* I could not find how to change host name. *我找不到如何更改主机名。

You can use -H in order to specify the host.您可以使用-H来指定主机。 This is the help message from next dev:这是来自下一个开发者的帮助信息:

Description
        Starts the application in development mode (hot-code reloading, error
        reporting, etc)

      Usage
        $ next dev <dir> -p <port number>

      <dir> represents the directory of the Next.js application.
      If no directory is provided, the current directory will be used.

      Options
        --port, -p      A port number on which to start the application
        --hostname, -H  Hostname on which to start the application (default: 0.0.0.0)
        --help, -h      Displays this message

the best and easiest way to config is the domains .最好和最简单的配置方法是 you need to paste the code bellow to your next.config.js file and add the domain:您需要将下面的代码粘贴到您的next.config.js文件并添加域:

images: {
domains: ["example.domain.com"],
},

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

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