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:
-
I'm currently on Ubuntu 18 / nodebb 1.16.2 / Mongo DB 4.0.22 / npm 6.14.10
-
I've already read
-
I've looked at
- nodebb-plugin-friends on GitHub
- WriteAPI doc
- ReadAPI doc
-
and, in the end, I've created an API Access token bound to the forum administrator which works nicely to read and write in the forum (e.g. retrieving the topics or creating a new one)
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)
- why looking at the nodebb log (run as ./nodebb dev) I can't see
- 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
-
-
You're attaching to the wrong hook. You need to use
static:api.routes
NodeBB/src/routes/write/index.js at 949f043a966bca5dc04cc1c674dfca4132650f5c · NodeBB/NodeBB
Node.js based forum software built for the modern web - NodeBB/src/routes/write/index.js at 949f043a966bca5dc04cc1c674dfca4132650f5c · NodeBB/NodeBB
GitHub (github.com)
It looks like the friends plugin integrates with the write API plugin, not the new write API.
-
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
-
@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.