Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

node.js - How to run the server simultaneously with the client in a nodeJS application using concurently when the files are in different folders

My project is split into 2 folders client and server in the client folder the usual react-create-app package.json in sever folder package.json with trace script

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

how can I run the script through npm start to run both the backend and the frontend at the same time

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

I found a solution that you can start from the server folder as follows

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

but how to start if package.json is in different folders

question from:https://stackoverflow.com/questions/65880555/how-to-run-the-server-simultaneously-with-the-client-in-a-nodejs-application-usi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you need to cd into your root directory or where both client and server folders are and npm install, npm init, git init, touch gitignore and add /node_modules

In your package.json in your root directory add this:

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

In your server directory, npm install and touch gitignore /nodemodules

In your package.json add this:

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

},

In your client folder, npm install and touch gitignore /node_modules

In your package.json add this:

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

}

Then you can always run your app and server by just cd into your main directory, and doing npm run dev.

Btw: Its hard to deploy servers to heroku when they are separated into a folder and they aren't in a root directory.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...