Clearing up a httpNode.post

Hey,

I'm working on a node, which is listening on an endpoint which is set up using a RED.httpNode.post call. I'm having an issue where after deploy the node stops outputting messages, but I am getting a response on the http call.

I also find that removing the node and deploying will still get responses from the node via HTTP (where the HTTP-In node will stop responding if it's deleted and deployed).

My suspcion is that it's the HTTP Post from first load is being kept open, and we need to close this HTTP Post listener on deploy before re-opening it again.

This code on HTTP In seems to clean itself up, but when I run it on my node, _router.routes is empty

this.on("close",function() {
                var node = this;
                RED.httpNode._router.stack.forEach(function(route,i,routes) {
                    if (route.route && route.route.path === node.url && route.route.methods[node.method]) {
                        routes.splice(i,1);
                    }
                });
            });

What am I doing wrong? There's seems to be no documentation on how to use RED.httpAdmin.post

This is my code:

        var obj = RED.httpAdmin.post(('/webhook/' + node.id), function(req, res){  WebhookCallback(req, res); } );

        this.on('close', function() {
            console.log("close");
            // tidy up any state
            obj = undefined;
            var node = this;
                RED.httpNode._router.stack.forEach(function(route,i,routes) {

                        routes.splice(i,1);
  
                });
        });

I've tried in this example just removing everything for debugging, but it still persists

The RED.httpAdmin.post object is an Express.js application. They don't provide a built-in way of removing a route - hence the code that delves into the internal _router object. Because it delves into the internals of express, it is possible they have changed something and the code we have in the HTTP In node needs updating.

The other approach for you to consider is to not add an HTTP end point for each instance of your node - just add it once at the top level of your file and use the a path parameter for the node id so you can then delegate any request to the appropriate node instance if it exists...

RED.httpAdmin.post('/webhook/:nodeId', function(req,res) {
   var nodeId = req.params.nodeId;
   var node = RED.nodes.getNode(nodeId);
   if (node) { 
      // call the callback function you have added to the individual node instance.
      node.handleWebhookCallback(req,res);
   } else {
      res.send(404).end();
   }
});
1 Like

Aha, that's perfect that's solved the issue! Thanks

You can check out my close processing in uibuilder. That has a utility function that will remove things from the routing table. It was a right pain to get right.

1 Like