I have a simple TCP server that listens on a port.
var net = require(\"net\"); var server = net.createServer(function(socket) { socket.end(\"Hello!\\n\"
If you want to stop your server with npm stop or something like this. You can write the code that kill your server process as:
npm stop
require('child_process').exec(`kill -9 ${pid}`)
Check this link for the detail: https://gist.github.com/dominhhai/aa7f3314ad27e2c50fd5