expressjs / expressjs/express

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

Open
#3,945 5 comments 2 reactions 0 assignees View on GitHub
ideas
Dominant language
JavaScript
Stars
69.5k
Forks
25k
Avg merge
4d 20h
Merged PRs (30d)
9

Description

**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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.