Do not assume rawBody middleware is last in stack when moving it

This commit is contained in:
Nick O'Leary
2025-10-10 11:35:27 +01:00
parent f60f12cf47
commit 471f0d8bea

View File

@@ -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) {