Search code examples
node.jsgulpwebserverelectronnode-webkit

NodeJS: How to stop a web server?


I'm using the plugin gulp-server-livereload.

var server = require('gulp-server-livereload');
gulp.src(pathDir).pipe(server({
   livereload: true,
   directoryListing: true,
   open: true,
   port: 80
}));

How to stop the server?

I tried server.restart(), server.kill(), server.reset()

But always there are such errors:

Uncaught Error: listen EADDRINUSE 127.0.0.1:35729 at Object.exports._errnoException (util.js:1022:11) at exports._exceptionWithHostPort (util.js:1045:20) at Server._listen2 (net.js:1262:14) at listen (net.js:1298:10) at doListening (net.js:1397:7) at GetAddrInfoReqWrap.asyncCallback [as callback] (dns.js:62:16) at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:81:1

0)

Please help me to solve my question or tell me another option how you can implement what I need.


Solution

  • This error is occurring because of another process using the port. It is likely just a previous instance of this process, however it is possible that another application is using the port.

    You will be unable to stop this using Node, as it is a process remaining from another run and processes do not have the permissions to 'manage' one another.

    On Windows use task manager to kill the node process, on Mac you can use Activity Monitor, and on Unix/Linux you can use htop or similar.


    Alternatively on Unix/Linux (incl. MacOS) you can use

    sudo lsof -i :35729
    

    to find the process id of the process ID using that port, then kill it:

    kill -9 {PID}
    

    To ensure your node server cleanly shuts down in future add event handlers:

    process.on('SIGTERM', ..)
    
    process.on('uncaughtException', ..)
    

    and invoke code inside these to shut down your application.