Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
Overriding Expressjs response in middleware is not working

I want to modify response body before returning it, i've used solution from this answer

But it didn't work, here's my code:

app.js

function modify(req, res, next) {
  var json = res.json;
  console.log("step 1");
  res.json = function(data) {
    console.log("step 2");
    json.call(this, "modified");
  };
  next();
}
app.use(modify);

in router:

router.get('/',(req,res,next)=>{
  res.json('Welcome')
  next()
})

Middleware has been called but the override function (step 2) was never invoked, please tell me what's wrong with my code, thanks!

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Use next() to only for passing to next function

router.get('/',(req,res,next)=>{
    if(wantToStop=='true'){
       res.json('Welcome')
    }else{
      next()
   }

 })
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!