My Express 4.13.1应用程序侦听2个不同的端口

时间:2015-11-16 14:08:43

标签: node.js express

我遇到了一些奇怪的事情。可能是因为我是Express和Node的新手。

我正在使用Express 4.13.1,默认情况下它在端口3000上运行。

然而,当我将这一行添加到我的app.js:

/usr/local/bin/node --debug-brk=62380 --nolazy bin/www Debugger listening on port 62380 Mon, 16 Nov 2015 13:59:13 GMT chatDel:server Listening on port 3000

该应用程序开始侦听端口3000和8080。 控制台输出是:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var fs = require('fs');
var autoIncrement = require('mongoose-auto-increment');
var config = require('./config/serverConfig');

var routes = require('./routes/index');
var users = require('./routes/users');
var inmsg = require('./routes/inmsg');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);
app.use('/inmsg', inmsg);
app.listen(8080);


var connection =             mongoose.createConnection(config.mongoose.connectionString);
global.db = connection;
autoIncrement.initialize(connection);

//load all files in model dir
fs.readdirSync(__dirname + '/models').forEach(function(filename){
  if(~filename.indexOf('.js')) require(__dirname + '/models/' + filename);
});


// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});

module.exports = app;

但要明确的是,两个端口的请求都有回应。

我错过了什么?

这是我的app.js

{
  "name": "chatDel",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
"body-parser": "~1.13.2",
"cookie-parser": "~1.3.5",
"debug": "~2.2.0",
"express": "~4.13.1",
"indicative": "^1.3.1",
"jade": "~1.11.0",
"mongodb": "~3.0.7",
"mongoose": "^4.2.4",
"mongoose-auto-increment": "^5.0.1",
"morgan": "~1.6.1",
"serve-favicon": "~2.3.0",
"stately.js": "^1.3.0"
  }
}

这是我的Package.jason:

#!/usr/bin/env node

/**
 * Module dependencies.
 */

var app = require('../app');
var debug = require('debug')('chatDel: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);
}

修改 我采取了更多行动:

  1. 我重新启动了计算机
  2. 我确保没有“节点”进程存活
  3. 我再次从我的IDE开始调试
  4. 同样,两个端口都可用(8080和3000),并且在我的计算机的进程列表中只能看到1个“节点”进程。(我正在使用macbook pro)
  5. 当我更改侦听端口的行时:app.listen(3000)我在我的控制台中调试时出现以下错误:(同样,只有1个节点进程可见,然后在大约10秒后退出从错误。)

      

    / usr / local / bin / node --debug-brk = 49558 --nolazy bin / www   调试器侦听端口49558   端口3000已在使用中

         

    使用退出代码1完成处理

    编辑2: 这是我的www文件,它位于/ bin / www下它没有扩展名,虽然它是一个js文件,它是作为安装的一部分生成的。

    {{1}}

1 个答案:

答案 0 :(得分:1)

正如评论主题中所解释的那样 - 检查你的/ bin / www而不是app.js. :)