Chen Yaacov
Chen Yaacov

Reputation: 31

Run server side and front side at the same time

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 my package.json I 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 is a file in the folder back-end-server that I want to run at the same time with React folder: front-end-react, but they don't run at the same time when I call npm run dev.

Upvotes: 3

Views: 2124

Answers (3)

fedeteka
fedeteka

Reputation: 963

You can use an NPM Package:

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

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

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

In this case, the backend and front are different folders for the Node and React projects.

Upvotes: 3

Bonzo
Bonzo

Reputation: 443

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

Upvotes: 2

shiv
shiv

Reputation: 1952

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.

Upvotes: 2

Related Questions