用于退出的命令是什么?(即终止Node.js进程)


当前回答

按Ctrl+C两次或退出。

> 
(To exit, press ^C again or type .exit)
> 

其他回答

退出

let exitCode = 1;
process.exit(exitCode)

有用的退出代码

1 - Catchall for general errors
2 - Misuse of shell builtins (according to Bash documentation)
126 - Command invoked cannot execute
127 - “command not found”
128 - Invalid argument to exit
128+n - Fatal error signal “n”
130 - Script terminated by Control-C
255\* - Exit status out of range

在命令行中,.ext是您想要的:

$ node
> .exit
$

它记录在REPL文档中。REPL(Read Eval Print Loop)是Node命令行的名称。

在正常程序中,使用process.ext([code])。

我有一个应用程序,我想:

向用户发送电子邮件退出并返回错误代码

我必须将process.ext(代码)挂接到退出事件处理程序,否则邮件将不会被发送,因为调用process.ext(代码)直接杀死异步事件。

#!/usr/bin/nodejs
var mailer = require('nodemailer');
var transport = mailer.createTransport();
mail = {
  to: 'Dave Bowman',
  from: 'HAL 9000',
  subject: 'Sorry Dave',
  html: 'Im sorry, Dave. Im afraid I cant do <B>THAT</B>.'
}
transport.sendMail(mail);
//process.exit(1);
process.on('exit', function() { process.exit(1); });

如果您在Unix终端或Windows命令行中,并希望退出节点REPL,请选择。。。

按Ctrl+C两次,或键入.ext并按Enter键,或在行开头按Ctrl+D(仅限Unix)

调用全局进程对象的退出方法:

process.exit()

从文档中:

process.ext([exitcode])使用指定的代码结束进程。如果省略,则退出并返回“成功”代码0。要退出并显示“失败”代码:进程.退出(1);执行节点的shell应该看到退出代码为1。