run server side and front side at the same time

Viewed 805

How to run client and server code at the same time?

i have a folder "back-end-server" (Node.js) and another folder "front-end-react" (React.js).

in a packeage.json i'm tried to write:

"scripts": {

"server-dev": "nodemon --prefix back-end-server server.js",
"client-dev": "npm start --prefix front-end-react",
"dev": "concurrently \"npm run server-dev\" \"npm run client-dev\""

},

"server.js" this file in a folder "back-end-server" that I want to run at the same time with React folder: "front-end-react".

but this not run when i'm try - "npm run dev".

thank you!

3 Answers

You need to have them running in different shells on different ports. The other solution could be simply build front-end files and depoy as static files on a web server like Apache and Nginx which are configured to contact backend.

You can use a NMP Package:

https://www.npmjs.com/package/concurrently

The you use scripts on package.json one for the client, one for the server and using concurrently run both at the same time (on differents ports)

"scripts": {
"start": "node backend/server",
"server": "nodemon backend/server",
"client": "npm start --prefix frontend",
"dev": "concurrently \"npm run server\" \"npm run client\"",
 },

on this case backend and frontend are different folders for the Node and React proyects

If I understand it correctly, you are trying to run both the apps but due to port clash they are not starting.

if that's the case you can change the port, something like this:

npm start --port 3002
Related