# How to start 2 node servers

**URL:** <https://discourse.processing.org/t/how-to-start-2-node-servers/23390>\
**Category:** Libraries\
**Created:** [August 20, 2020, 2:03pm UTC](https://discourse.processing.org/t/how-to-start-2-node-servers/23390 "2020-08-20T14:03:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephanschulz](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@stephanschulz](https://discourse.processing.org/u/stephanschulz)\
**Post date:** [August 20, 2020, 2:03pm UTC](https://discourse.processing.org/t/how-to-start-2-node-servers/23390/1 "2020-08-20T14:03:59Z")

</div>

I am using p5js and ml5js to run a local project that tracks a person and saves images to a local folder.  
For this to work I have to use [https://github.com/ml5js/ml5-data-and-models-server](https://github.com/ml5js/ml5-data-and-models-server) to serve the ml models locally and start an express server to allow for file down and upload.  
Currently I manually start both in the terminal

```auto
cd ml5-data-and-models-server
npm run serve

```

and just `npm start server.js` in the folder that has my server.js file.

I am hoping to find a way to automatically start both. But simply starting them via the same shell script does not work since the first server blocks the rest of the script to be run.

```auto
#!/bin/bash

echo "$(date) : start file upload server"
cd /Users/stephanschulz/Documents/javascript/alphaBlend/alphaBlend_js
npm start server.js

echo "$(date) : start ml5js server"
cd /Users/stephanschulz/Documents/javascript/ml5-data-and-models-server-source
# npm install
npm run serve

```

I tried these 4 solutions but was not able to make them work, probably due to missing knowledge.  
[https://itnext.io/4-solutions-to-run-multiple-node-js-or-npm-commands-simultaneously-9edaa6215a93](https://itnext.io/4-solutions-to-run-multiple-node-js-or-npm-commands-simultaneously-9edaa6215a93)

Does anyone have any advice?  
Thanks a bunch.

---

<div class="post-metadata">

**Author:** ![stephanschulz](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@stephanschulz](https://discourse.processing.org/u/stephanschulz)\
**Post date:** [August 20, 2020, 2:18pm UTC](https://discourse.processing.org/t/how-to-start-2-node-servers/23390/2 "2020-08-20T14:18:34Z")

</div>

Using this package.json and calling `npm run start` does not work. It only starts the server.js that handles my file uploading but does not doe the `cd` and start the ml5js model server.

Here the excerpt that I changed

```auto
  "scripts": {
      "dev": "concurrently --kill-others \"cd ml5-data-and-models-server-source && npm run serve\" \"npm run start\""
  },

```

Here the full package.json

```auto
{
  "//1": "describes your app and its dependencies",
  "//2": "https://docs.npmjs.com/files/package.json",
  "//3": "updating this file will download and update your packages",
  "name": "alphaBlend",
  "version": "0.0.1",
  "description": "A simple Node app built on Express, instantly up and running.",
  "main": "server.js",
  "scripts": {
      "dev": "concurrently --kill-others \"cd ml5-data-and-models-server-source && npm run serve\" \"npm run start\""
  },
  "dependencies": {
    "body-parser": "^1.19.0",
    "concurrently": "^5.3.0",
    "cors": "^2.8.5",
    "crypto": "^1.0.1",
    "express": "^4.17.1",
    "formidable": "latest",
    "mime": "^2.4.6",
    "morgan": "^1.10.0",
    "multer": "^1.4.2",
    "path": "^0.12.7"
  },
  "engines": {
    "node": "12.x"
  },
  "repository": {
    "url": "https://antimodular.com/"
  },
  "license": "MIT",
  "keywords": [
    "node",
    "express"
  ],
  "devDependencies": {
    "npm-run-all": "^4.1.5"
  }
}

```

---

<div class="post-metadata">

**Author:** ![stephanschulz](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@stephanschulz](https://discourse.processing.org/u/stephanschulz)\
**Post date:** [August 20, 2020, 3:19pm UTC](https://discourse.processing.org/t/how-to-start-2-node-servers/23390/3 "2020-08-20T15:19:32Z")

</div>

here my unsuccessful second attempt at s shell script that I am trying to start like this without the need to `cd` to the project folder.  
`Stephan:~ stephanschulz$ sh /Users/stephanschulz/Documents/javascript/alphaBlend/alphaBlend_js/start_it.sh `

```auto
#!/bin/bash

echo "$(date) : start it"
echo $PWD
SCRIPTPATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
echo $SCRIPTPATH

echo "$(date) : start ml5js server"
(cd $SCRIPTPATH/ml5-data-and-models-server-source && npm run serve) & npm start $SCRIPTPATH/server.js

```

---

<div class="post-metadata">

**Author:** ![stephanschulz](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@stephanschulz](https://discourse.processing.org/u/stephanschulz)\
**Post date:** [August 21, 2020, 1:46am UTC](https://discourse.processing.org/t/how-to-start-2-node-servers/23390/4 "2020-08-21T01:46:55Z")

</div>

my very smart and good friend [Roy](https://github.com/roymacdonald) worked it out, a shell script that spans 2 threads, one per server.

```auto
#!/bin/bash
trap "exit" INT TERM ERR
trap "kill 0" EXIT

echo "$(date) : start it"
echo $PWD
SCRIPTPATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
echo $SCRIPTPATH

echo "$(date) : start ml5js server"
#cd /Users/stephanschulz/Documents/javascript/ml5-data-and-models-server-source
cd "$SCRIPTPATH/ml5-data-and-models-server-source"
# npm install
npm run serve &
ML5_PID=$!
echo "ML5_PID: $ML5_PID\n"

echo "$(date) : start file upload server"
#cd /Users/stephanschulz/Documents/javascript/alphaBlend/alphaBlend_js/server
cd "$SCRIPTPATH/server"
npm start server.js &
SERVER_PID=$!
echo "SERVER_PID: $SERVER_PID"

wait

```
