How to stop app that node.js express 'npm start'

后端 未结 16 2018
名媛妹妹
名媛妹妹 2020-12-07 11:00

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?

16条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-07 11:15

    When I tried the suggested solution I realized that my app name was truncated. I read up on process.title in the nodejs documentation (https://nodejs.org/docs/latest/api/process.html#process_process_title) and it says

    On Linux and OS X, it's limited to the size of the binary name plus the length of the command line arguments because it overwrites the argv memory.

    My app does not use any arguments, so I can add this line of code to my app.js

    process.title = process.argv[2];
    

    and then add these few lines to my package.json file

      "scripts": {
        "start": "node app.js this-name-can-be-as-long-as-it-needs-to-be",
        "stop": "killall -SIGINT this-name-can-be-as-long-as-it-needs-to-be"
      },
    

    to use really long process names. npm start and npm stop work, of course npm stop will always terminate all running processes, but that is ok for me.

提交回复
热议问题