NodeJS和CoffeeScript咖啡可执行文件的行为不一样吗?

问题描述

| 我有一个简单的test.coffee,可以编译为test.js 测试咖啡
process.on \'uncaughtException\',(er) ->
  console.log \'Unhandled exception\'
  console.log \'Unhandled exception\'
  console.log \'Unhandled exception\'
  console.log \'Unhandled exception\'
  console.log \'Unhandled exception\'
  console.log \'Unhandled exception\'
  console.log \'Unhandled exception\'
  console.log \'Unhandled exception\'
  return

throw new Error(\"aAH\")
和产生的test.js
(function() {
  process.on(\'uncaughtException\',function(er) {
    console.log(\'Unhandled exception\');
    console.log(\'Unhandled exception\');
    console.log(\'Unhandled exception\');
    console.log(\'Unhandled exception\');
    console.log(\'Unhandled exception\');
    console.log(\'Unhandled exception\');
    console.log(\'Unhandled exception\');
    console.log(\'Unhandled exception\');
  });
  throw new Error(\"aAH\");
}).call(this);
从命令行或从vim通过(!node%和!coffee%)等。输出令人惊讶地不同。 节点test.js正常运行,并向控制台输出一些未处理的异常行并退出。通过\'coffee test.coffee \'调用可以返回到打印堆栈跟踪并退出认行为。该示例显然不是我的完整应用程序,但是在做一个更大的应用程序时,当我通过coffee boot.cofee启动Expressjs应用程序时,无法处理未处理的异常,我在做什么错?这是Mac OS X 10.6.x上的最新Node 0.4.8和最新Cofee 1.1.1     

解决方法

        通过
coffee
命令运行CoffeeScript代码时,该代码将编译为JS,然后在Node进程中以编程方式运行。特别是,CoffeeScript使用以下命令
mainModule._compile code,mainModule.filename
(请参阅coffee-script.coffee),其中“ 4”是对“ 5”的引用。这就是为什么在堆栈跟踪中应该看到
Error: aAH
    at Object. (.:12:9)
    at Object. (.:13:4)
    at Module._compile (module.js:404:26)
    ...
您碰到的一个副作用是,异常永远不会一直降到
process
级别。取而代之的是,由于此代码,它变得难以捉摸
  try
    ...
    else if o.run         then CoffeeScript.run t.input,t.options
    ...
  catch err
    ...
在command.coffee中。 在运行
coffee foo.coffee
时,CoffeeScript采取几个步骤来模拟\“ pure \” Node.js进程,但是直接运行CoffeeScript和运行已编译的JS之间总会有一些差异。对于您正在开发的复杂应用程序,我建议设置一个Cakefile,以便您可以在保存时自动重新编译,测试和运行应用程序,而不是使用编辑器的内置运行命令。     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...