node.js 为开发环境分配一个域名给localhost

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7576217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:38:14  来源:igfitidea点击:

Assigning a domain name to localhost for development environment

node.jsmacosssldnsssl-certificate

提问by fixedpoint

I am building a website and would not like to reconfigure the website from pointing to http://127.0.0.1to http://www.example.com. Furthermore, the certificate that I am using is of course made with the proper domain name of www.example.combut my test environment makes calls to 127.0.0.1which makes the security not work properly.

我建立一个网站,并不会像从指向重新配置的网站http://127.0.0.1http://www.example.com。此外,我使用的证书当然是使用正确的域名制作的,www.example.com但我的测试环境会调用127.0.0.1这使得安全性无法正常工作。

What I currently want to do is configure my development environment to assign the domain name www.example.comto 127.0.0.1so that all http://www.example.com/xyzis routed to http://127.0.0.1:8000/xyzand https://www.example.com/xyzis routed to https://127.0.0.1:8080/xyz.

我现在想要做的就是配置我的开发环境将域名转让www.example.com127.0.0.1使所有http://www.example.com/xyz被路由到http://127.0.0.1:8000/xyzhttps://www.example.com/xyz路由到https://127.0.0.1:8080/xyz

I am notusing Apache. I am currently using node.js as my web server and my development environment is in Mac OS X Lion.

没有使用 Apache。我目前使用 node.js 作为我的 Web 服务器,我的开发环境在 Mac OS X Lion 中。

回答by fourk

If you edit your etc/hosts file you can assign an arbitrary host name to be set to 127.0.0.1. Open up /etc/hosts in your favorite text editor and add this line:

如果你编辑你的 etc/hosts 文件,你可以指定一个任意的主机名来设置为 127.0.0.1。在您喜欢的文本编辑器中打开 /etc/hosts 并添加以下行:

127.0.0.1   www.example.com

Unsure of how to avoid specifying the port in the HTTP requests you make to example.com, but if you must avoid specifying that at the request level, you could run nodejs as root to make it listen on port 80.

不确定如何避免在向 example.com 发出的 HTTP 请求中指定端口,但如果必须避免在请求级别指定端口,则可以以 root 身份运行 nodejs 以使其侦听端口 80。

Edit: After editing /etc/hosts, you may already have the DNS request for that domain cached. You can clear the cached entry by running this on the command line.

编辑:编辑 /etc/hosts 后,您可能已经缓存了对该域的 DNS 请求。您可以通过在命令行上运行来清除缓存的条目。

dscacheutil -flushcache