Nodejs express的./bin/www入口,怎么样才能正常执行?

Nodejs express的./bin/www入口,怎么样才能正常执行?

今天弄了一天的服务器搭环境centOS + nginx + nodejs + mongodb,重装不下5次,终于看到调试通过,访问正常。幸好随手做记录省去各种麻烦。 现在各种小问题中,一直在用node app.js,一堆问题还不能访问,原来express入口文件变成“./bin/www”,直接node ./bin/www运行还是错。 只能用:# DEBUG=name ./bin/www 那些个supervisor、forever 都不能用了,运行一堆错误,好不方便。

这个是DEBUG的,怎么样才能系统启动时正常执行项目而非DEBUG。


8 回复

Node.js Express 的 ./bin/www 入口,怎么样才能正常执行?

今天我花了一整天时间来配置服务器环境(CentOS + Nginx + Node.js + MongoDB),重装不下5次,终于看到调试通过,访问正常。幸好我随手做了记录,省去了很多麻烦。

现在遇到了一些小问题,一直使用 node app.js 运行应用,但出现了各种问题导致无法访问。后来发现 Express 应用的入口文件已经变成了 ./bin/www,直接运行 node ./bin/www 仍然报错。

只有使用 DEBUG=name ./bin/www 命令才能正常运行,但是那些常用的进程管理工具如 supervisorforever 却无法正常使用,运行时出现一堆错误,非常不方便。

如何让系统启动时正常执行项目而非在 DEBUG 模式下运行?

解决方案

  1. 修改 ./bin/www 文件中的默认启动方式: 默认情况下,./bin/www 文件中可能包含了对 DEBUG 环境变量的检查。你可以修改这部分代码,使其不依赖于 DEBUG 环境变量。

  2. 设置环境变量: 在启动 Node.js 应用之前,可以设置必要的环境变量,确保应用在正确的环境中运行。

  3. 使用 pm2 替代 supervisorforeverpm2 是一个更强大的进程管理工具,支持热重启、日志管理和监控等功能。推荐使用 pm2 来管理你的 Node.js 应用。

示例代码

假设你的 ./bin/www 文件内容如下:

#!/usr/bin/env node

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

为了去掉对 DEBUG 环境变量的依赖,可以注释掉或删除与 debug 相关的代码,例如:

// var debug = require('debug')('name:server');

然后保存并重新启动应用:

# 设置环境变量
export PORT=3000

# 使用 pm2 启动应用
pm2 start ./bin/www

这样就可以在不依赖 DEBUG 环境变量的情况下正常启动应用了。


看一下报错原因,应该是代码问题吧

node bin/www是可以运行的

我终于搞定了,用forever /home/www/app_name/bin/www,用chkconfig方式实现了自启动。 现在终于可以专心研究程序了,反复弄了一天,还好我过来了。。 那些错误,完全不知道什么玩意,只是给出了一些文件出错的行。实际错误根本不在那里。 nodejs启动实在蛋疼,希望以后像PHP一样安完环境自解析。

或者 你cd到www目录 node www也可以

这不管nodejs什么事儿,如果在当前文件夹下,node ./bin/www是有效地,我一直这么用

新版express使用npm start 运行。 入口参数设置在package.json中: “start”: “node ./bin/www”

所以还是node ./bin/www

直接npm start 不久行了吗。。

在使用Express框架时,默认的入口文件通常设置为./bin/www。这个文件用于启动应用服务器,并且包含了一些配置和初始化逻辑。要让./bin/www正常工作,可以参考以下步骤:

1. 确认 package.json 中的启动脚本

确保你的package.json文件中定义了正确的启动命令。通常,start字段应该指向./bin/www文件,例如:

{
  "name": "your-app",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    // ...
  }
}

2. 检查 ./bin/www 文件

确保./bin/www文件包含正确的逻辑来启动你的应用。一个典型的./bin/www文件可能看起来像这样:

#!/usr/bin/env node

/**
 * Module dependencies.
 */
var app = require('../app');
var debug = require('debug')('your-app-name: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);
}

3. 使用正确的工具管理进程

如果你希望使用类似supervisorforever这样的工具来自动重启应用,你需要确保这些工具安装正确并且配置正确。例如,使用supervisor,你可以这样做:

npm install -g supervisor
supervisor ./bin/www

或者使用forever

npm install -g forever
forever start ./bin/www

4. 避免 DEBUG 环境变量的影响

如果你不想使用DEBUG环境变量,可以在启动应用时取消设置它:

DEBUG="" node ./bin/www

或者在启动脚本中明确禁用它:

"scripts": {
  "start": "DEBUG= node ./bin/www"
}

通过以上步骤,你应该能够顺利地启动并运行你的Express应用。

回到顶部