node.js - robots.router 和 robots.http 有什么区别?

标签 node.js coffeescript hubot

我继承了一个coffeescript hubot应用程序。除了自述文件和教程之外,我找不到论坛或任何其他问题的答案,所以我在这里提问。

我用谷歌搜索了这个问题,但找不到答案。

robot.router和robot.http有什么区别?两者似乎都采用 get、put、delete 等和 URL。我的应用程序同时使用这两种方法,但在我看来,我看不出其中的区别。

看起来路由器正在运行express,而http是自制的。这对客户端来说有语义上的差异吗?

最佳答案

我也很难找到好的文档,但设法从 Hubot Scripting doc 中获得了一些见解。 。如果您在页面中搜索“robot.router”和“robot.http”,您将遇到以下定义:

机器人.http

这用于对其他 Web 服务进行 HTTP 调用(类似于 jQuery AJAXAxios)

Hubot can make HTTP calls on your behalf to integrate & consume third party APIs. This can be through an instance of node-scoped-http-client available at robot.http. The simplest case looks like:

robot.http("https://midnight-train")   
  .get() (err, res, body) ->
  # your code here

机器人.路由器

这是一个Express服务器。它是一个 HTTP 监听器,用于接受和响应 HTTP 请求

Hubot includes support for the express web framework to serve up HTTP requests. It listens on the port specified by the EXPRESS_PORT or PORT environment variables (preferred in that order) and defaults to 8080. An instance of an express application is available at robot.router. It can be protected with username and password by specifying EXPRESS_USER and EXPRESS_PASSWORD. It can automatically serve static files by setting EXPRESS_STATIC.

The most common use of this is for providing HTTP end points for services with webhooks to push to, and have those show up in chat.

module.exports = (robot) ->
  # the expected value of :room is going to vary by adapter, it
  # might be a numeric id, name, token, or some other value
  robot.router.post '/hubot/chatsecrets/:room', (req, res) ->
    room   = req.params.room
    data   = if req.body.payload? then JSON.parse req.body.payload else req.body
    secret = data.secret
    robot.messageRoom room, "I have a secret: #{secret}"
    res.send 'OK'

关于node.js - robots.router 和 robots.http 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49416502/

相关文章:

javascript - 按需更新多个 Highcharts 实例的最佳性能

backbone.js - 主干模型保存,验证失败

linux - 如何从 hubot 执行 shell 脚本

node.js - Hubot - 从文件加载环境变量

node.js - 我在哪里可以看到 hubot 记录器消息?

node.js - 将查询快照中的文档作为 json 字符串 firestore 返回

node.js - 防止 Mongoose findOneAndUpdate 中的 updatedAt 变化

node.js - 如何捕获像 EADDRINUSE 这样的 node.js/express 服务器错误?

node.js - 监听其他应用程序中发生的事件

javascript - 使用 q.js Promise 时出现问题