如何使用域名而不是带有create-react-app的url中的“ localhost:3000”进行本地开发? [英] How can I develop locally using a domain name instead of 'localhost:3000' in the url with create-react-app?

查看:211
本文介绍了如何使用域名而不是带有create-react-app的url中的“ localhost:3000”进行本地开发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发一个带有create-react-app和我添加的其他一些库的小型应用程序。给定计划的架构和一些我想与react-router一起运行的测试,我希望能够使用url中的域mame在本地运行我的应用程序,从而代替

I have been developing a small app with create-react-app and a few other libraries i added. Given the planned architecture and some tests i'd like to run with react-router, i would like to be able to run my app locally using a domain mame in the url, so that instead of

localhost:3000 /

localhost:3000/

它将是

somedomain.com

somedomain.com

我尝试了一些操作,例如弹出ceate-react-app,但是我不确定如何修改webpack配置以更改公共路径,或者如何在其上设置某种代理我的机器上,所以somedomain.com相当于键入localhost:3000

I have tried a few things like ejecting ceate-react-app but im not sure how to modify the webpack config to change the public path, or how to set up some sort of proxy on my machine so somedomain.com would be equivalent to typing localhost:3000

我不知道正确的方法是什么,我似乎还找不到任何可靠的信息。

I'm not sure what the correct approach is and i cant seem to find any reliable information yet.

推荐答案

以及如上所述更改 / etc / hosts ,如果您希望CRA知道新的主机名,则 HOST env var应该会为您提供所需的内容。

As well as changing your /etc/hosts as advised above, if you want CRA to know about the new host name, the HOST env var should give you what you want.

例如(这可以放在 scripts / start 下的 package.json 中)

E.g. (this can go into your package.json under scripts/start)

HOST=somedomain.com react-scripts start

这应该启动基于CRA的开发服务器,侦听该域名,并且它应该打开/刷新指向该域名的浏览器选项卡。

That should start your CRA-based dev server, listening at that domain name, and it should open/refresh a browser tab pointing there.

编辑:有关CRA使用的此环境变量和其他环境变量的文档

这篇关于如何使用域名而不是带有create-react-app的url中的“ localhost:3000”进行本地开发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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