In Node.js, I want to kill a server running on port 1337
. process.kill(pid)
seems like the way to do that. But how do I get the pid of the server running on port 1337
, in Node.js?
I see plenty of examples of how to do this with ps
and lsof
. Is there a way to pull this off without relying on shell commands in Node.js?
Here is how I would do this by relying on lsof
:
Number(child_process.execSync('lsof -i :1337 -t'))