UNSOLVED Issue on reaching out to new routes established using the WriteAPI


  • Hi community first and foremost thank for your valuable support.

    I need to retrieve custom data I've written to mongoDB and I'd like them to be returned as JSON string via a custom WriteAPI-enabled route.

    A few notes to give a better context:

    Issues:

    • Upon installing nodebb-plugin-friends (clone and npm link) and activating in a pristine test forum I can see the hook in Advanced -> Hooks but:
      • why looking at the nodebb log (run as ./nodebb dev) I can't see [plugins/friends] Write API integration enabled, routes added. as specified here?
      • assuming I'm using Postman for testing purposes how should i reach these routes? (none of these - <url/to/nodebb>/api/v3/friends/:uid, <url/to/nodebb>/api/friends/:uid, <url/to/nodebb>/friends/:uid, <url/to/nodebb>api/v3/plugins/friends/:uid - worked for me)
    • Upon creating a test plugin (see below library.js and plugin.json) I'm not able to reach any of the established new routes (as much as reported above): how can I debug the issue? Log is silent and

    library.js

    'use strict';
    
    (function(routes) {
    
      routes.initWriteRoutes = function(data, callback) {
    
        data.router.get('/route1', data.apiMiddleware.requireUser, data.apiMiddleware.requireAdmin, middleware.verifyUserExists, function(req, res) {
          console.log("/route1 reached via get");
          res.sendStatus(200);
        });
    
        data.router.post('/route2', data.apiMiddleware.requireUser, middleware.verifyUserExists, function(req, res) {
          console.log("/route2 reached via post");
          res.sendStatus(200);
        });
    
        data.router.delete('/route3', data.apiMiddleware.requireUser, middleware.verifyUserExists, function(req, res) {
          console.log("/route3 reached via delete");
          res.sendStatus(200);
        });
    
        console.log('[plugins/test] Write API integration enabled, routes added.');
        callback(null, data);
      };
    
    }(module.exports));
    

    plugin.json

    
    {
      "library": "./library.js",
      "hooks": [
          { "hook": "filter:plugin.write-api.routes", "method": "initWriteRoutes" }
      ]
    }
    
    • assuming nodebb-plugin-friends is installed why the log reports info: [api] Adding 0 route(s) to 'api/v3/plugins' with 0 new routes? Shouldn't it include at least the new routes coming with this plugin?

    I apologise for the length of this post, but I think this might help you to steer me in the right direction.

    Best, Riccardo

  • Global Moderator

    You're attaching to the wrong hook. You need to use static:api.routes

    https://github.com/NodeBB/NodeBB/blob/949f043a966bca5dc04cc1c674dfca4132650f5c/src/routes/write/index.js#L53

    It looks like the friends plugin integrates with the write API plugin, not the new write API.

  • GNU/Linux Admin

    @pitaj is correct, the friends plugin is likely adding new routes to api/v2, the Write API plugin (if it is installed and enabled).

    If you want it to add routes to v3, you'll need to use the hook he suggested.


  • Thanks a lot @PitaJ and @julian, changing the hook worked.

    Moving to api/v3 looks like I can't use what @julian mentioned

    It passes in router, apiMiddleware, middleware, and errorHandler, so given that you can style your own routes like I do in the write api, e.g.:

    var addRoutes = function(data, callback) {
      data.router.get('/awesome', apiMiddleware.requireUser, function(req, res) {
        console.log('lorem ipsum!');
        res.sendStatus(200);
      });
    
      callback(null, data);
    });
    

    In the above example, I used the requireUser middleware, which, provided by the write-api, means you have to pass in an user/master token just like the other routes. It also sets req.user.uid for you to use.

    How can I check that the bearer Token coming with the req.header is valid and corresponds to a certain user to which the bearer token is bound to?

    Any snippet to look at?

    Thanks, Riccardo

  • GNU/Linux Admin

    @knickknack I've updated the friends plugin to compatibility with latest NodeBB.

    This is the commit that introduces write API v3 integration

    Perhaps this will give you some pointers.


  • @julian that's awesome thanks a lot!

Suggested Topics

  • 2
  • 1
  • 11
  • 18
  • 11
| |