title: Nodejs express server not closing
body: <p>I have an express nodejs server with express-generator. After calling "npm start" server starts properly but I'm not able to close the connection to localhost. Typical exiting the process with "ctrl+c" doesn't work as expected. After "ctrl+c" I'm not able to start the server on the same localhost port again. I'm still using Win7, my node is up to date and other globally installed packages. <strong>My console prints out following:</strong></p>
  
  <pre><code>$ npm start
  
  &gt; myapp@0.0.0 start C:\Users\Konrad\dev\myapp
  &gt; node ./bin/www
  
  Port 3000 is already in use
  npm ERR! code ELIFECYCLE
  npm ERR! errno 1
  npm ERR! myapp@0.0.0 start: `node ./bin/www`
  npm ERR! Exit status 1
  npm ERR!
  npm ERR! Failed at the myapp@0.0.0 start script.
  npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
  
  npm ERR! A complete log of this run can be found in:
  npm ERR!     C:\Users\Konrad\AppData\Roaming\npm-cache\_logs\2018-01-19T20_02_46_983Z-debug.log
  </code></pre>
  
  <p><strong>Thats the code in bin/www:</strong></p>
  
  <pre><code>#!/usr/bin/env node
  
  /**
   * Module dependencies.
   */
  
  var app = require('../app');
  var debug = require('debug')('myapp:server');
  var http = require('http');
  
  /**
   * Get port from environment and store in Express.
   */
  
  var port = normalizePort(process.env.PORT || '3000');
  app.set('port', port);
  
  /**
   * Create HTTP server.
   */
  
  var server = http.createServer(app);
  
  /**
   * Listen on provided port, on all network interfaces.
   */
  
  server.listen(port);
  server.on('error', onError);
  server.on('listening', onListening);
  
  /**
   * Normalize a port into a number, string, or false.
   */
  
  function normalizePort(val) {
    var port = parseInt(val, 10);
  
    if (isNaN(port)) {
      // named pipe
      return val;
    }
  
    if (port &gt;= 0) {
      // port number
      return port;
    }
  
    return false;
  }
  
  /**
   * Event listener for HTTP server "error" event.
   */
  
  function onError(error) {
    if (error.syscall !== 'listen') {
      throw error;
    }
  
    var bind = typeof port === 'string'
      ? 'Pipe ' + port
      : 'Port ' + port;
  
    // handle specific listen errors with friendly messages
    switch (error.code) {
      case 'EACCES':
        console.error(bind + ' requires elevated privileges');
        process.exit(1);
        break;
      case 'EADDRINUSE':
        console.error(bind + ' is already in use');
        process.exit(1);
        break;
      default:
        throw error;
    }
  }
  
  /**
   * Event listener for HTTP server "listening" event.
   */
  
  function onListening() {
    var addr = server.address();
    var bind = typeof addr === 'string'
      ? 'pipe ' + addr
      : 'port ' + addr.port;
    debug('Listening on ' + bind);
  }
  </code></pre>