You build node.js app with express v4.x then start your app by npm start. My question is how to stop the app? Is there npm stop?
EDIT to include the error when implement npm stop
/home/nodetest2# npm stop
> nodetest2@0.0.1 stop /home/nodetest2
> pkill -s SIGINT nodetest2
pkill: invalid argument for option 's' -- SIGINT
npm ERR! nodetest2@0.0.1 stop: `pkill -s SIGINT nodetest2`
npm ERR! Exit status 2
All the other solutions here are OS dependent. An independent solution for any OS uses socket.io as follows.
package.json
has two scripts:server.js - Your usual express stuff lives here
server.stop.js
Test it out
npm start
(to start your server as usual)npm stop
(this will now stop your running server)The above code has not been tested (it is a cut down version of my code, my code does work) but hopefully it works as is. Either way, it provides the general direction to take if you want to use socket.io to stop your server.
You have to press combination ctrl + z My os is Ubuntu
Yes, npm provides for a stop script too:
npm help npm-scripts
Set one of the above in your package.json, and then use
npm stop
npm help npm-stop
You can make this really simple if you set in
app.js
,And, then in scripts.json,
That said, if this was me, I'd be using
pm2
or something the automatically handled this on the basis of a git push.