簡體   English   中英

Node.js Express服務器未關閉

[英]Nodejs express server not closing

我有一個帶有express-generator的express nodejs服務器。 調用“ npm start”后,服務器可以正常啟動,但是我無法關閉與localhost的連接。 使用“ ctrl + c”退出進程通常無法正常工作。 在“ ctrl + c”之后,我無法再次在同一本地主機端口上啟動服務器。 我仍在使用Win7,我的節點是最新的,並且安裝了其他全局安裝的軟件包。 我的控制台將輸出以下內容:

$ npm start

> myapp@0.0.0 start C:\Users\Konrad\dev\myapp
> 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

多數民眾贊成在bin / www中的代碼:

#!/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 >= 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);
}

因此,為避免此問題,我開始使用名為“ nodemon”的軟件包。 它刷新了我的應用程序的代碼,而無需重新啟動服務器。

該問題仍未解決,但我希望沒有其他人遇到此問題。 可能是我的錯誤加上一些Windows7不兼容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM