0

我正在尝试在 coffeescript 中处理一个 http 请求,但如果服务器关闭,应用程序就会因下面的错误而死掉,而且我找不到正确的解决方案。

代码:

 http.get "http://localhost:8080/health", (res) ->
        status =  res.statusCode
        value = if status == 200 then 1 else 0
        console.log value
        server.push_metric metricPrefix , value
        res.on 'error', () ->
          colsone.log "Tomcat Disconected"

错误:

events.js:71
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: connect ECONNREFUSED
    at errnoException (net.js:770:11)
    at Object.afterConnect [as oncomplete] (net.js:761:19)
4

1 回答 1

1

我认为您必须在单独的事件处理程序中主动侦听错误。现在,您正在将一个事件处理程序附加到响应 ( res),但它需要附加到请求对象本身。请参阅文档

req = http.get "http://localhost:8080/health", (res) ->
  status = res.statusCode
  value = if status == 200 then 1 else 0
  console.log value
  server.push_metric metricPrefix , value

req.on 'error', ->
  console.log "Tomcat Disconected"

此外,您当前的错误处理程序中有一个错字:colsone.log

于 2013-04-09T16:19:36.800 回答