I have the this declaration in my httpNodeMiddleware function:
var jwt = require('jsonwebtoken');
Where can I move this too so that I don't have to load the module every time a request comes in?
cheers,
I have the this declaration in my httpNodeMiddleware function:
var jwt = require('jsonwebtoken');
Where can I move this too so that I don't have to load the module every time a request comes in?
cheers,
In settings.js file
https://nodered.org/docs/user-guide/writing-functions#loading-additional-modules
You don't really need to worry too much about that actually since require won't reload the module. That's one of the nice features. You can load the same module in lots of places within a node.js app and it will only ever load it once.
That's how the singleton class module approach works.
Hi Steve,
Thanks for your reply, but I believe that only works for functions created in your flow, not for functions in the settings.js file itself.
Thanks for your reply that is really good to know.
After sleeping on it I came up with two answers to my own problem
httpNodeMiddleware: (function(){
var jwt = require('jsonwebtoken');
return function(req,res,next) {
//jwt object is accessible from here and is not reinitialized every time the function is called.
};
})(()
One word of caution, if you can get way with using the module pattern then it is the much safer approach, as any variables you add are only accessible within your module. If you use the second approach and modify settings.js directly then this could cause issues in the future if the Node-Red developers add a property to settings.js that you've already used for something else.
cheers
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.