最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I change localhost:3000 to custom.domain in react - Stack Overflow

programmeradmin2浏览0评论
 "scripts": {
    "start": "cross-env NODE_PATH=src react-scripts start",
    "build": "cross-env NODE_PATH=src react-scripts build",
  }

How do I change localhost:3000 to custom.domain in react

 "scripts": {
    "start": "cross-env NODE_PATH=src react-scripts start",
    "build": "cross-env NODE_PATH=src react-scripts build",
  }

How do I change localhost:3000 to custom.domain in react

Share Improve this question asked Jan 3, 2022 at 9:52 Farid siadatzadehFarid siadatzadeh 611 gold badge1 silver badge2 bronze badges 1
  • you want to change port right ? 3000 to other one ? – Mayur Vaghasiya Commented Jan 3, 2022 at 9:54
Add a comment  | 

5 Answers 5

Reset to default 11

If you are asking about purely locally serving your application you can simply append a HOST value before the script. Much like others have pointed out you can do with a PORT value.

"scripts": {
  "start": "HOST=custom.domain react-scripts start"
}

Then on your machine you will just need to ensure that you have configured your hosts files with the appropriate directive

127.0.0.1 custom.domain

Assuming you have run the start script and are serving the site you can now access it via the url http://custom.domain:3000

In host file add following lines.

Windows: C:\Windows\System32\Drivers\etc\hosts

Linux: /etc/hosts

127.0.0.1 mydomain.local
127.0.0.1 subdomain.mydomain.local

Add HOST in .env file

// ..
HOST=mydomain.local
// ..

Run React App

npm run start

Visit: mydomain.local

run below command to start the server

HOST=admin.localhost && npm start

server will start on the below url

http://admin.localhost:3000/

you can replace admin.localhost with what you want

If you want to change domain you need to upload your react app to web space. If you want to change port you need to send argument in start script.

"scripts": {
    "start": "set port=4213 && react-scripts start",
}

In this case port will be 4213. So site will run on http://localhost:4213

You can change the port by doing the following change for the start command in your package.json file.

"scripts": {
    "start": "PORT=8050 react-scripts start",
  }

If you want to connect to a custom domain then hosting is the best option.

Other option is to first change the PORT no and then use some service like ngrok to expose that port.

发布评论

评论列表(0)

  1. 暂无评论