Node and Express:如何实现基本的webhook服务器 [英] Node and Express: How to implement basic webhook server

查看:95
本文介绍了Node and Express:如何实现基本的webhook服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找到教程.除了对它们应该如何工作的一些基本描述之外,我对Webhooks还是陌生的,没有使用或看过它们.

I'm having a surprisingly hard time finding tutorials. I'm new to webhooks and have not used or seen them, beyond some basic descriptions of how they're supposed to work.

我们的用例是在有新记录时更新我们API的用户.由于我们使用的是Kafka,并且已经确定了最终一致性",因此另一种用法是,当无法从Kafka流中正确读取/写入记录时,将错误通知给他们.

Our use-case for this is updating users of our APIs when there are new records. Since we're using Kafka and have settled on "eventual consistency" , another use might be to notify them of errors when records could not be read/written properly from the Kafka stream.

据我所知,基本概念是

const express = require("express");
const router = express.Router();

const processSomething = callback => {
    setTimeout(callback, 20000);
}

router.post("/hook", (req, res, next) => {
    processSomething(() => {
        res.status(200).send({
            id: "ABC123",
            message: "New record added!"
        });
    });
});

module.exports = router;

这实际上是我要拍摄的吗?用户是否会发布到此端点,等待响应,然后在获得响应后再次发布以重新订阅?长时间甚至无限期运行是否有问题?

Is this essentially what I'm shooting for? Would users post to this endpoint, await a response, then post again upon getting the response so as to re-subscribe? Is there a problem with this running for a long period or even indefinitely?

我确实可以使用更复杂的示例,但我只是找不到它们.在Google搜索时发现的大多数内容都涉及到集成第三方Webhook,例如Github,Slack等,而不是我需要构建的自定义.

I could really use more sophisticated examples but I'm just not finding them. Most of what you find when Googling this involves integrating third-party webhooks, like Github, Slack, etc., not custom which is what I'm needing to build.

我也不完全反对另一种方法.只是寻找通知API用户有关更新和其他重要信息的最佳方法.

I'm not opposed to another approach entirely, either. Just searching for the best means of notifying API users of updates and other important info.

推荐答案

Webhooks尝试以延迟的事件通知用户,但是他们以异步方式进行操作.您的代码是同步的,这意味着用户需要等待答复.我认为它应该看起来像这样

Webhooks try to notify users with delayed events but they do it in asynchronous way. Your code is synchronous that means users need to wait for reply. I think it should looks more like this

const express = require("express");
const router = express.Router();

const processSomething = callback => {
  setTimeout(callback, 20000);
}

router.post("/hook", (req, res, next) => {
  processSomething(() => {
    const webhookUrl = req.params.url;

    /**
     * Your Kafka action or something else. There
     * you should collect info about success or
     * fail of client's action.
     */

    /** 
     * Your API call to webhookUrl with 
     * your defined body about status of event
     */
  });

  res.status(200).send('OK')
});

module.exports = router;

其中函数 processSomething 以异步方式执行操作,并以响应方式调用客户端的API,但您的路由仍以响应方式回答- 200 OK 表示您从客户端收到了消息.

where function processSomething do your action in asynchronous way and call client's API with response but your route still answer with response - 200 OK means you got the message from client.

您还可以将有关成功/失败消息的通知代理委托给代码的另一个位置,但是您需要为此保存 webhookUrl .在事件系统中,您可以创建事件(或简单消息),其中属性之一是客户端的url.

Also you can delegate notyfing client about success/fail message to another place of your code but you need to save webhookUrl for that. In events system, you can create event (or simple message) where one of attributes is client's url.

这篇关于Node and Express:如何实现基本的webhook服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆