How can I develop locally using a domain name instead of 'localhost:3000' in the url with create-react-app?

安稳与你 提交于 2020-05-23 10:46:49

问题


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/

it would be

somedomain.com

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.


回答1:


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.

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

HOST=somedomain.com react-scripts start

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

Edit: docs on this and other env vars that CRA uses.




回答2:


You can do it by updating your hosts file with the following entry

127.0.0.1 somedomain.com

and then use somedomain.com to access your site

But if somedomain.com happens to be your actual host address then you will have to revert it back when you want to connect to the actual hosted somedomain.com

You can find more info about how to edit your host file here: https://www.siteground.com/kb/how_to_use_the_hosts_file/

There a similar answer which you can find here: Assigning a domain name to localhost for development environment




回答3:


you can change the port by adding

"scripts": {
    "start": "set PORT=8080 && react-scripts start"
}

but there is no other way to change the IP address when you are developing locally.i also tried to do that.



来源:https://stackoverflow.com/questions/51146086/how-can-i-develop-locally-using-a-domain-name-instead-of-localhost3000-in-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!