(原创)Node.JS编程,实现一个“不死”程序!( 三 )

consthttp=require(\"http\");

constpid=process.pid;

http.createServer(function(reqres){

console.log(`Handing request from${pid`);

res.end(`Hello from${pid\\n`);

).listen(8000function(){

console.log(`Started${pid`);

)

//秒数后触发 , 主动抛出一个错误

setTimeout(function(){

thrownewError(\"ooops\");

Math.ceil(Math.random()*3) *1000);

此代码 , 会在启动后数秒抛出错误 , 当发生错误、程序退出时 , 主进程会自动重启一个工作进程 。

效果如下:

推荐阅读