我正在尝试获取使用NodeJs中EventSource时出现的错误信息,以下示例可能更有助于您理解:
var url = 'http://api.example.com/resource'
var EventSource = require('eventsource');
var es = new EventSource(url);
es.onmessage = function(e) {
console.log(e.data);
};
es.onerror = function(event) {
console.log(event);
};
在 onerror 函数中,我希望能够获取有关错误的信息,但是
event
为空或未定义,以及< strong> es
对象(好吧,这个对象只是一对花括号< code> {} )。如果出错,我想读取响应头,类似于:es.onerror = function(e) {
console.log(e.header.location);
};
这个有可能吗?我错过了什么吗?我认为答案应该很简单,但是我在NodeJs上还比较新。
EventSource
,然后订阅了onmessage
和onerror
事件。 - Gepser Hoil