如何使用一个命令同时运行我的节点应用程序和反应应用程序?

Ter*_*ong 6 node.js reactjs

我将为 React 和 Node 构建样板,根文件夹结构如下所示

    - (root level)
    - client - src
             - public
             - package.json
             - ... 

    - package.json
    - server.js
    - node_modules
Run Code Online (Sandbox Code Playgroud)

在根文件夹的 package.json 中,我有一些代码,例如:

 "start": " nodemon ./server.js | react-scripts start ./client"
Run Code Online (Sandbox Code Playgroud)

当我运行时npm start,只有反应应用程序被构建并运行,节点应用程序不运行。有关如何运行npm startReact 和 Node 应用程序的任何提示吗?

对了,我已经"proxy": "http://localhost:5000/"在客户端的package.json中设置了。server.js 的代码如下

const port = process.env.PORT || 5000;

// console.log that your server is up and running
app.listen(port, () => console.log(`Listening on port ${port}`));
Run Code Online (Sandbox Code Playgroud)

谢谢。


解决方案:

非常感谢罗德里戈普的回复。

我使用发包来解决我的问题。

我的package.json根文件夹看起来像这样并且可以工作:

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "concurrently \"cd client && npm run start\" \"npm run start\"",
    "start": "nodemon ./server.js"
  },
Run Code Online (Sandbox Code Playgroud)

bha*_*esh 2

我找到了两种方法

1 通过命令

"scripts": {
    "start": "react-scripts start",
    "dev": "(cd server && npm run start) & npm run start"
  }
Run Code Online (Sandbox Code Playgroud)

2 通过 npm 包

  • 同时进行npm
  • npm 运行所有npm