【问题标题】:How to run the server simultaneously with the client in a nodeJS application using concurently when the files are in different folders当文件位于不同文件夹中时,如何在nodeJS应用程序中同时运行服务器和客户端
【发布时间】:2021-01-25 15:40:52
【问题描述】:

我的项目分为 2 个文件夹 客户端和服务器 在客户端文件夹中通常 react-create-app package.json 在服务器文件夹中 package.json 带有跟踪脚本

scripts: {
 start: node app.js
server: nodemon app.js
}

如何通过 npm start 运行脚本以同时运行后端和前端

client
  -app.js
  -package.json
server
  -app.js
  -package.json

我找到了一个解决方案,你可以从服务器文件夹开始如下

dev: "concurently" npm run server "" npm run client "

但是如果 package.json 在不同的文件夹中如何开始

【问题讨论】:

  • 指向其他目录的相对路径?

标签: node.js reactjs express npm


【解决方案1】:

您需要 cd 到您的根目录或客户端和服务器文件夹所在的位置以及 npm install、npm init、git init、touch gitignore 并添加 /node_modules

在根目录的 package.json 中添加:

"client": "cd client && npm start",
"backend": "cd server && nodemon server.js",
"dev": "concurrently \"npm run backend\" \"npm run client\"",

在你的服务器目录中,npm install 并触摸 gitignore /nodemodules

在你的 package.json 中添加:

"scripts": {
"start": "node server.js",
"server": "nodemon server.js"

},

在您的客户端文件夹中,npm install 并触摸 gitignore /node_modules

在你的 package.json 中添加:

  "scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"

}

然后,您始终可以通过 cd 进入主目录并执行 npm run dev 来运行您的应用程序和服务器。

顺便说一句:当服务器被分离到一个文件夹中并且它们不在根目录中时,很难将它们部署到 heroku。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    • 2019-01-21
    • 1970-01-01
    • 2018-01-05
    相关资源
    最近更新 更多