【问题标题】:How to make nodejs and react use different ports如何让nodejs和react使用不同的端口
【发布时间】:2020-10-08 11:42:38
【问题描述】:

package.json 文件:

"start": "concurrently \"nodemon index.js --port 3000 \" \"react-scripts start --port 3000 \" ",
"start": "concurrently \"react-scripts start --port 3000\" ",
    "build": "react-scripts build",
    "server": "NODE_ENV=production node index.js",

所以我可以同时运行两个 nodejs 服务器并做出反应。使用npm run servernpm start 或仅使用npm start,包括两者

但是因为我在 .env 文件中有 PORT=3001 server 和 react 都尝试在 3001 服务器上运行。当然它会给出错误“某些东西已经在端口 3001 上运行。”

如何让 react 拥有自己的 3000 端口?选项 --port 3000 没有帮助

【问题讨论】:

    标签: node.js reactjs react-fullstack


    【解决方案1】:

    您必须在 CLI 上或通过您的脚本明确提供端口。

    {
      "start:client": "PORT=3001 react-scripts start",
      "start:server": "PORT=3000 nodemon index.js",
      "start": "concurrently \"start:client\" \"start:server\""
    }
    

    这应该更容易阅读。

    【讨论】:

      【解决方案2】:

      您可以覆盖每个脚本的环境变量。

      例如:

      {
        "start": "concurrently \"PORT=3000 nodemon index.js\" \"PORT=3001 react-scripts start\""
      }
      

      【讨论】:

      • 好。有用。否则,为什么反应从 .env 获取端口变量?我从来没有在任何地方指定它?它发生在哪个代码部分选择 3001 而不是默认的 3000?
      • 我使用 "proxy": "localhost:3001",也在那个地址上运行 react 应用程序,即使它在 3000 上。为什么我没有让它出现在那里?
      • react-scripts 使用 .env by default,从 0.5.0 版本开始
      猜你喜欢
      • 2021-06-16
      • 1970-01-01
      • 2017-04-29
      • 1970-01-01
      • 1970-01-01
      • 2012-09-15
      • 2022-01-10
      • 2012-06-25
      • 2021-12-16
      相关资源
      最近更新 更多