【问题标题】:How can I start my node server and react app at the same time?如何同时启动我的节点服务器和响应应用程序?
【发布时间】:2019-06-23 12:36:06
【问题描述】:

我使用 express 创建了一个 API,我想在我的前端服务器中使用它,问题是为了让我的 api 工作,我必须不断地在服务器上运行它。但是我不能同时运行我的反应应用程序。所以我的问题是如何同时启动我的反应服务器和 api?

附:我同时尝试了,但我对如何让它工作感到困惑,这是我的 package.json 中的一些示例代码

{
    "name": "app",
    "version": "0.1.0",
    "private": true,
    "dependencies": {
    "@material-ui/core": "^4.1.1",
    "@material-ui/icons": "^4.2.0",
    "axios": "^0.19.0",
    "concurrently": "^4.1.0",
    "express": "^4.17.1",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-scripts": "3.0.1"
  },
     "scripts": {
     "start": "node src/connection",
     "build": "react-scripts build",
     "test": "react-scripts test",
     "eject": "react-scripts eject",
     "react": "react-scripts start",
     "dev": "concurrently \"npm start\" \"npm react\""
     },
     "proxy": "http://localhost:3000",
    "eslintConfig": {
    "extends": "react-app"
    },
    "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": []
 }
}

【问题讨论】:

标签: node.js reactjs concurrently


【解决方案1】:

分 5 个步骤:

  1. 安装npm i --s concurrently
  2. Node/server/backend's package.json 中添加以下脚本
    "client": "npm start --prefix client",
    "clientinstall": "npm install --prefix client",
    "dev": "concurrently \"npm run server\" \"npm run client" 
  1. 确保您为客户端和服务器提供了正确的路径
  2. 运行“npm run dev”
  3. 微笑

【讨论】:

    【解决方案2】:

    安装包 npm-run-all,它可以帮助您执行多个脚本。 您可以参考以下链接:

    https://www.npmjs.com/package/npm-run-all

    安装此软件包后, 在你的 package.json 中,添加这样的脚本:

    "scripts": {    
      "start-js": "react-scripts start",
      "backend-start": "NODE_ENV=production node node_api/server.js",
      "start": "concurrently \"npm-run-all -p backend-start start-js\"",
      "build": "npm-run-all build-css && react-scripts build",
      "test": "react-scripts test",
      "eject": "react-scripts eject"
    }
    

    在这里,当您运行命令“npm start”时,它首先运行“backend-start”脚本,该脚本启动您的后端服务器,然后它开始响应。

    只需更改“backend-start”脚本中的路径即可。 用你的路径节点启动文件替换“node_api/server.js”

    【讨论】:

      【解决方案3】:

      npm-run-all 包将帮助您完成这项任务。

      可以在 Create React App 的 package.json 中设置一个选项,将非 text/html 请求代理到另一个后端。您可以使用此功能代理在其他地方运行的应用程序,使用此功能您将能够在 React 项目本身内运行服务器。

      在 package.json 文件中添加以下行:"proxy": "http://localhost:3001",

      修改脚本部分如下:

          "scripts": {
          "start": "react-scripts start",
          "server": "node-env-run server --exec nodemon",
          "dev": "run-p server start",
          "build": "react-scripts build",
          "test": "react-scripts test",
          "eject": "react-scripts eject"
        }
      

      最后你的 package.json 文件会变成这样。

      {
                "name": "frontend_backend",
                "version": "0.1.0",
                "private": true,
                "dependencies": {
                  "@testing-library/jest-dom": "^5.11.8",
                  "@testing-library/react": "^11.2.2",
                  "@testing-library/user-event": "^12.6.0",
                  "nodemon": "^2.0.6",
                  "npm-run-all": "^4.1.5",
                  "react": "^17.0.1",
                  "react-dom": "^17.0.1",
                  "react-scripts": "4.0.1",
                  "web-vitals": "^0.2.4"
                },
                "proxy": "http://localhost:3001",
                "scripts": {
                  "start": "react-scripts start",
                  "server": "node-env-run server --exec nodemon",
                  "dev": "run-p server start",
                  "build": "react-scripts build",
                  "test": "react-scripts test",
                  "eject": "react-scripts eject"
                },
                "eslintConfig": {
                  "extends": [
                    "react-app",
                    "react-app/jest"
                  ]
                },
                "browserslist": {
                  "production": [
                    ">0.2%",
                    "not dead",
                    "not op_mini all"
                  ],
                  "development": [
                    "last 1 chrome version",
                    "last 1 firefox version",
                    "last 1 safari version"
                  ]
                }
              } 
      

      现在,使用npm run dev 运行应用程序。 (您可以将“dev”更改为您想要的任何内容,例如:"app": "run-p server start",然后使用npm run app 命令运行应用程序)

      【讨论】:

      • 很好的解决方案!谢谢!
      【解决方案4】:
      1. 在后端安装 npm concurrently

      2. 在后端的 package.json 中添加以下脚本

        "start": "node server.js",
        "server": "nodemon server.js",
        "frontend": "cd ./frontend && npm run dev",
        "dev": "concurrently \"npm run server\" \"npm run frontend\""
        
      3. 确保您为客户端和服务器提供了正确的路径

        添加 "proxy": "localhost:5000" 作为你的 react 应用的 package.json 文件的代理。假设您的 nodejs 应用程序在端口 5000 上运行。

      4. 从后端根文件夹运行“npm run dev”

      【讨论】:

        猜你喜欢
        • 2016-06-15
        • 1970-01-01
        • 1970-01-01
        • 2018-05-28
        • 1970-01-01
        • 2015-07-22
        • 1970-01-01
        • 2018-09-10
        • 1970-01-01
        相关资源
        最近更新 更多