Write a plugin that call new restful API on server


  • Anime Lovers

    hi experts,

    I am a newbie, so pardon for if my question is noob. Basically, I want to write a plugin / widget that will query a new restful api that i will be to implement.

    Do I need to:

    1. modify the nodebb's core to add the restful apis or
    2. implements at the plugin's library.js and it will auto-bind the new restful api at the server (is this possible?).

    Thanks and appreciated.


  • Community Rep

    Hello, welcome to NodeBB!

    This should be easy to implement without core modifications.

    For the API you are querying, are you going to be creating it within the plugin or as a separate app on the server?

    If you are making the API within the plugin itself, you can use the express router which is passed in the init hook. You can use to just like any other express app in this way. The Router also has additional middlewares already attached that will give you req.user etc..

    plugin.init = function(params, next) {
      var router = params.router;
      router.get('/pluginname/a/restful/:route', function (req, res, next) {
        res.json({
          result: 'some awesome data'
        })
      });
    }
    

    Then just have the widget query it with jquery,

    $.getJSON('/pluginname/a/restful/:route', function(data){ console.log(data.result); });
    

    This is how my widgets update their data in Minecraft Integration.

    If the API is a separate app, I don't think you can do that transparently. You would do it as above, and use the Request module to query the actual data.


  • Anime Lovers

    Thanks @yariplus.

    I want to write a new restful api that will query a new table (mongodb document) on the same server that nodebb resided. The data will be populated by a cron job running on the server.

    I guess I understand what you meant. Let me go and have a look on your widgets implementation. Thanks for the help!


  • Community Rep

    @Zinca-Bruce No problem! Let me know if you any additional help. :)


Log in to reply
 

Looks like your connection to NodeBB was lost, please wait while we try to reconnect.