express icon indicating copy to clipboard operation
express copied to clipboard

why not make middleware or hooks that works exactly before sending response that could change or alter the response object

Open abdoolly opened this issue 5 years ago • 5 comments

Problem statement

There is a problem which always happens what if we wanted to make certain actions or wrap our responses all our responses in express in a certain standard way right now there is no built in hooks or middlewares which works before the sending

Lots of inefficient workarounds

I know there is workarounds by just overriding the response object but thats sometimes not efficient enough and sometimes breaks some functions while responding so , why not make hooks for the main response functions and just pass in the req and res objects for the user to modify and just return the res and req after the modification.

Just a proposed way for the way to make such hooks

let express = require('express');
let app = express();
let bodyParser = require('body-parser');

// some middlewares

app.use(bodyParser());

// routes here
app.get('/test',(req,res) => res.send('I am a test function'));

// now put a hook in the app to run before the send
// this is an example of how to make send hook 
app.hooks.onSend = (req,res) => {

    res.body = {
            body: res.body,
            status : 200
   };

   return { res, req };
};

benefits

It is a simple use case but it will help alot of people make their code way better. by at least making a way to change res object either in headers or in the body or any other thing according to what they need to change.

abdoolly avatar Apr 24 '19 10:04 abdoolly

Hi @abdoolly,

Thanks for the detailed and well reasoned request. While I can see your reasoning, I think that what you are asking for is not the domain of Express to solve. This is a great use case for a wrapper library which exposes the interface you desire. This is a common way for users to solve problems where Express is still used under the hood. Also, it is the way the Express generally team prefers because it means we do not have to maintain the code for this new interface (smaller core).

Does that make sense?

wesleytodd avatar May 10 '19 16:05 wesleytodd

@abdoolly Is this what you want?

jiangwei1995 avatar Jun 12 '19 09:06 jiangwei1995

@jiangwei1995 : Could you please have a look at: https://github.com/prkeshri/express/commit/db50bddde2beb1e1ae3c0b4489c6a421a4c0ffef/ If this could provide some value, I will raise a pull request. Thanks.

prkeshri avatar Jun 13 '19 20:06 prkeshri

I've just made a library called node-ally, you could submit this as a feature request

Grantlyk avatar Jun 23 '19 22:06 Grantlyk

I think this is a valid request.

Express middleware is limited by the fact that the control flows downwards to other middlewares or handlers until the response is sent.

It is a useful feature to be able to capture certain actions so that people can control different application behaviours. For instance, users may need to wrap their request-response pipeline into one Unit of Work so that they can either commit or rollback based on the response before sending the response.

ASP.NET framework supports this kind of behaviour but sadly express.js doesn't.

danieljee avatar Mar 27 '20 08:03 danieljee