Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在节点进程退出时发送http请求_Node.js_Http_Process_Request_Exit - Fatal编程技术网

Node.js 在节点进程退出时发送http请求

Node.js 在节点进程退出时发送http请求,node.js,http,process,request,exit,Node.js,Http,Process,Request,Exit,我已经编写了示例节点应用程序来处理错误数据,例如数据库连接错误、端口冲突错误、进程未捕获异常。发生错误时,会发出http请求以处理错误。在这种情况下,当节点进程异常存在时,我可以在进程中处理exist。在('exit')函数中,但我无法发送http请求,进程将快速退出 任何人都可以建议如何在进程退出之前发送http请求并在Node.js上获得响应。下面是在进程存在时发送http请求的示例代码 var http = require('http'); var errorData=null; var

我已经编写了示例节点应用程序来处理错误数据,例如数据库连接错误、端口冲突错误、进程未捕获异常。发生错误时,会发出http请求以处理错误。在这种情况下,当节点进程异常存在时,我可以在
进程中处理exist。在('exit')
函数中,但我无法发送http请求,进程将快速退出

任何人都可以建议如何在进程退出之前发送http请求并在Node.js上获得响应。下面是在进程存在时发送http请求的示例代码

var http = require('http');
var errorData=null;
var sendErrorReport = function(data,callback){
    var options = {
        host : connection.host,
        path : "/api/errorReport",
        port : connection.port,
        method : 'POST',
        timeout: connection.timeInterval,
        headers:{
            'Content-Type':'application/json',
            'Content-Length': Buffer.byteLength(data)}
    }
    var request =  http.request(options,function(response){
        callback(response);
    });
    request.on('error',function(err){
        console.log("On Error");
        callback(err);
    });
    request.on('timeout', function(err){console.log("On Timeout");
        callback(err);});
    request.write(data);
    request.end();
}
process.on('uncaughtException', function ( err ) {
    errorData = err;
});
process.on('exit',function(code){
    sendErrorReport(errorData,function(err,res){
        console.log(res);
    });

})
process.on('exit',[fn])
中,不能执行中所述的任何异步操作。然而,在许多库中也发现了这种反模式

您需要依赖于
进程。在('uncaughtException',[fn])
进程中的任何信号处理程序,如
SIGTERM
。在('exit',[fn])
上,您不能执行中所述的任何异步操作。然而,在许多库中也发现了这种反模式

您需要依赖于
进程。在('uncaughtException',[fn])
或任何信号处理程序(如
SIGTERM
)上遇到相同的问题, 根据文件

“事件:'exit'侦听器函数只能执行同步操作。”这会使向其他系统发送请求变得困难

一种可能的解决方法是执行另一个脚本/cmd,例如

import exec from 'child_process'

mocha.run(failures => {
  process.on('exit', () => {
    exec.execSync(some cmd, function (error, stderr) {
      if (error) {
        throw (error);
      }
 }
}
碰到同样的问题, 根据文件

“事件:'exit'侦听器函数只能执行同步操作。”这会使向其他系统发送请求变得困难

一种可能的解决方法是执行另一个脚本/cmd,例如

import exec from 'child_process'

mocha.run(failures => {
  process.on('exit', () => {
    exec.execSync(some cmd, function (error, stderr) {
      if (error) {
        throw (error);
      }
 }
}

如果处理
SIGINT
uncaughtException
事件,则可以在请求的回调中调用
process.exit
。它应该一直挂起,直到您调用该
.exit()
函数为止。在某些情况下,我需要在未引发未捕获异常时处理进程退出。如果您处理
SIGINT
uncaughtException
事件,则可以在请求的回调中调用
process.exit
。它应该一直挂起,直到您调用该
.exit()
函数为止。在某些情况下,我需要在未引发未捕获异常时处理进程退出。