我有一个简单的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")
Run Code Online (Sandbox Code Playgroud)
和产生的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);
Run Code Online (Sandbox Code Playgroud)
从命令行或从vim通过(!node%和!coffee%)等。输出令人惊讶地不同。
节点test.js正常运行,并向控制台输出一些未处理的异常行并退出。通过'coffee test.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
Run Code Online (Sandbox Code Playgroud)
(请参阅coffee-script.coffee),其中mainModule对的引用require.main。这就是为什么在堆栈跟踪中,您应该看到
Error: aAH
at Object. (.:12:9)
at Object. (.:13:4)
at Module._compile (module.js:404:26)
...
Run Code Online (Sandbox Code Playgroud)
您遇到的这种情况的一个副作用是,异常永远不会一直下降到最低process水平。取而代之的是,由于此代码,它被抓住
try
...
else if o.run then CoffeeScript.run t.input, t.options
...
catch err
...
Run Code Online (Sandbox Code Playgroud)
在运行时,CoffeeScript采取了几个步骤来模拟“纯” Node.js进程coffee foo.coffee,但是直接运行CoffeeScript和运行已编译的JS之间始终会有一些差异。对于您正在开发的复杂应用程序,建议您设置一个Cakefile,以便您可以在保存时自动重新编译,测试和运行应用程序,而不是使用编辑器的内置运行命令。