From 471f0d8beaab845c7955b2705fead0d0c88fa6f4 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Fri, 10 Oct 2025 11:35:27 +0100 Subject: [PATCH] Do not assume rawBody middleware is last in stack when moving it --- .../@node-red/nodes/core/network/21-httpin.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/network/21-httpin.js b/packages/node_modules/@node-red/nodes/core/network/21-httpin.js index 4cbf03f39..d6f766c01 100644 --- a/packages/node_modules/@node-red/nodes/core/network/21-httpin.js +++ b/packages/node_modules/@node-red/nodes/core/network/21-httpin.js @@ -129,8 +129,17 @@ module.exports = function(RED) { if(typeof RED.httpNode === 'function' && (rootApp = getRootApp(RED.httpNode))) { // Add middleware to the stack rootApp.use(rawBodyCapture); - // Move the middleware to top of the stack - rootApp._router.stack.unshift(rootApp._router.stack.pop()); + // Find the newly added middleware and move it to the top of the stack + // Do not assume its the last entry in the stack as some frameworks (eg loopback) + // add middleware in particular orders + for (let i = 0; i < rootApp._router.stack.length; i++) { + const layer = rootApp._router.stack[i]; + if (layer && layer.handle === rawBodyCapture) { + // Move the middleware to top of the stack + rootApp._router.stack.unshift(rootApp._router.stack.splice(i, 1)[0]); + break; + } + } } function createRequestWrapper(node,req) {