Skip to content

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

Open
@abdoolly

Description

@abdoolly

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions