Search code examples
node.jscoffeescripthubot

What's the difference between robot.router and robot.http?


I have inherited a coffeescript hubot application. I can't find a forum or any other answers to my question outside the readme and tutorial, so I'm asking here.

I have googled this question and can't find an answer.

What is the difference between robot.router and robot.http? Both seem to take get, put, delete, etc. and a URL. My application uses both, but to my eye, I can't tell the difference.

It looks like router is running express and http is something homegrown. Does this make a semantic difference to the client?


Solution

  • I'm having a hard time finding good documentation too, but managed to get some insights from the Hubot Scripting doc. If you search the page for "robot.router" and "robot.http" you will come across the following definitions:

    robot.http

    This is for making HTTP calls to other web services (similar to jQuery AJAX or Axios)

    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
    

    robot.router

    This is an Express server. It's a HTTP listener for accepting and responding to HTTP requests

    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'