提问者:小点点

如何用NodeJs侦听和订阅HTTP事件?


我试图创建一个发票,然后通过订阅事件来跟踪它的状态。我正在为此使用bitpay REST API,并且我已经成功地创建了发票并成功地检索了总线令牌。

bitpay文档是这样写的:

Once you have retrieved the bus pass, It's pretty simple. Send a GET request to the path configured for the bus - let's say it's bitpay.com/events including the appropriate parameters:

?token= - your "bus pass" retrieved from the API
&action= - usually "subscribe", but options are provided in your bus pass
&events[]= - an array of events to listen for (also provided in the bus pass)
Once you've opened the connection, you'll want to listen for chunks of data to be passed to you. Each chunk represents an event. Since the bus adheres to the SSE/EventSource specification, chunks are passed back in the format:

event: <event_type>

data: {"some":"json"}

我如何收听这些事件?当我点击我正在尝试收听的链接时,我看到的是:

:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
retry: 2000
event: connect
data: {"status":"subscribed","resource":"Sg9hggVxgC1VTKsGYqNyVT","facade":"public/invoice","events":["payment","confirmation","paymentRejected","scanned","paymentPosted"]}

event: state
data: {"url":"https://test.bitpay.com/invoice?
...

Herre就是我试过的:

const evtSource = new EventSource('<path>');

evtSource.onmessage = (e) => {
    console.log(e);
}

axios({
    url : '<path>',
    method: 'get',
    connect: function(event){
        console.log(event);
    }
})

谢谢!


共1个答案

匿名用户

尝试如下所示:

https://github.com/axios/axios#示例

null

// Make a request for a user with a given ID
axios.get('https://jsonplaceholder.typicode.com/todos/1')
  .then(function (response) {
    // handle success
    console.log(response);
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  })
  .then(function () {
    // always executed
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.0/axios.min.js"></script>