1
  • I am deploying a Vue client using Vue-cli 3, which uses Webpack
    (I can start the client by calling "yarn dev --open")
  • I am also writing a server with an API for the client
    (I can start the server by calling "node server/server.js")

Is there a way to start both the client and the server with one command?
I was thinking that I should add some code to vue.config.js to start the server before the client is being compiled.

Preferably this would all work in a hot-reload way.


So far I tried a shell script as Alex78191 suggested:

#!/usr/bin/env bash

node server/server.js &
yarn dev --open

This works, but when I try to stop the servers using ctrl-C, only the yarn-process stops, but the node server keeps on running. Is there a way in bash to stop all started processes (background and foreground) with the ctrl-C command?

Hendrik Jan
  • 4,396
  • 8
  • 39
  • 75
  • You can create shell script. – Alex78191 Mar 05 '18 at 12:58
  • Basically `yarn dev --open; node server/server.js` – Mehdi Benmoha Mar 05 '18 at 13:28
  • The script you were using also works good but will not open anything because it's running in the background, the last `&` character is for running processes in the background. Try remove this character – Mehdi Benmoha Mar 05 '18 at 13:30
  • What makes you think that the client starts from the command line but not from the script? You use the same command in both cases, there should be no difference. The only difference is the `&`, which means that the script is not waiting this command to finish for starting the next one. – Pierre François Mar 05 '18 at 13:31
  • You are right, I can start both commands from a bash script. The only problem I am left with (see my latest edit) is that the background-process is not easily stoppable (using ctrl-C) and needs a lot of work in Windows-10 to stop. – Hendrik Jan Mar 05 '18 at 13:37

1 Answers1

0

I was able to get this working with the following bash script:

#!/usr/bin/env bash

node server/server.js &
pid=$!

yarn dev --open

trap "kill ${pid}; exit 1" INT

This script is a little bit more complex than you might expect to make sure that all child processes stop when this script stops (using ctrl-C). For more information on stopping child processes I found some help here: how-can-bash-script-do-the-equivalent-of-ctrl-c-to-a-background-task

Hendrik Jan
  • 4,396
  • 8
  • 39
  • 75