6

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

PS 我同时尝试了,但我对如何让它工作感到困惑,这是我的 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": []
 }
}
4

4 回答 4

4

分 5 个步骤:

  1. 安装npm i --s concurrently
  2. 在Node/server/backend 的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. 微笑
于 2019-11-05T21:30:23.927 回答
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”脚本来启动你的后端服务器,然后它开始反应。

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

于 2019-06-24T12:29:34.870 回答
0
  1. 在后端安装 npm并发包

  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”

于 2021-08-30T20:11:44.167 回答
0

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命令运行应用程序)

于 2021-01-03T17:15:06.780 回答