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

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Domain
api, backend

Research direction

The issue names no files, tests, or entry points. Begin by determining where Express handles response sending and how middleware is ordered, then establish whether a supported pre-send hook can safely alter headers or bodies and define tests for the intended behavior.

Written by the indexing model from the issue text.

Description

ideas

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.

Dominant language
JavaScript
Stars
69.7k
Forks
25.6k
Avg merge
2d 9h
Merged PRs (30d)
4

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from expressjs/express

All issues in expressjs/express

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.