最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Override Node.js Express response method - Stack Overflow

programmeradmin3浏览0评论

I want to slightly change default expressjs behaviour of res.json(obj) method. I am trying to override it in my own middleware, the thing is I need to call its original inside.

But now it just calls itself causing a stack overflow.

app.use(function(req, res, next) {
    res.json = function(obj) {
        function delete_null_properties(obj) {
            // ...
        }
        delete_null_properties(obj);

        res.json(obj);
    };
    next();
});

I want to slightly change default expressjs behaviour of res.json(obj) method. I am trying to override it in my own middleware, the thing is I need to call its original inside.

But now it just calls itself causing a stack overflow.

app.use(function(req, res, next) {
    res.json = function(obj) {
        function delete_null_properties(obj) {
            // ...
        }
        delete_null_properties(obj);

        res.json(obj);
    };
    next();
});
Share Improve this question edited Dec 6, 2015 at 6:54 CodeWizard 142k22 gold badges159 silver badges179 bronze badges asked Dec 6, 2015 at 6:51 simdsimd 2,0193 gold badges20 silver badges25 bronze badges 6
  • Why would you want to do that? I think you are fixing the wrong problem... can you explain what you want to achieve, ie what you need to change? – Sergio Commented Dec 6, 2015 at 6:53
  • @Sergio, It shown in the code. I want the same json, but without null keys. I want it to be transparent inside overall application, like default approach. – simd Commented Dec 6, 2015 at 6:57
  • But res.json will send response back to client side... and you have a next() after. You are interfering with one of Expresses's response to clienth methods. I am sure you can fix you problem in another way. – Sergio Commented Dec 6, 2015 at 6:59
  • 1 would it be anything like overriding the res.render method - stackoverflow./questions/9285880/… – Jaromanda X Commented Dec 6, 2015 at 7:12
  • @Sergio, why do you think interfering expresses's methods is bad? Do you think it could break functionality in modules that could strictly rely on this method? Anyway, I don't think there's much of a change to worry about in case of deleting nulls. – simd Commented Dec 6, 2015 at 7:31
 |  Show 1 more ment

3 Answers 3

Reset to default 18

I don't know the inner workings of express very well, but it seems something like this should work

app.use(function(req, res, next) {
    var json = res.json;
    res.json = function(obj) {
        function delete_null_properties(obj) {
            // ...
        }
        delete_null_properties(obj);

        json.call(this, obj);
    };
    next();
});

edit: changed json(obj) to json.call(this, obj) as per ment by user3537411 and this previous answer to a similar question

P.S. I started the answer with I don't know the inner workings of express very well to avoid the sort of ments that just put crap on an answer without really going into WHY an answer is bad ... instead I get the sort of ment that's equally pointless. You can't win with SO trolls

You can do like below, after const app = express(); dont use this.json and dont use this.send inside the function otherwise you will get a maximum call size error :

app.response.json = function(body: any) {
    this.contentType('json').end(JSON.stringify(
      {
          code: ApiCode.OPERATION_SUCCES,
          message: CommonMessages.OperationSuccess.message,
          data: body,
          description: CommonMessages.OperationSuccess.description
        }
    ));
    return this; 
}

It also might be useful

https://github./muratcorlu/connect-api-mocker/pull/30

Mounting twice will apply only last one.

const express = require('../../node_modules/express');

const app = express();

// default response
app.use('/', (req, res, next) => {
  next();

  try {
    res.send({
      profile: {
        first_name: 'Aaron',
        last_name: 'Pol'
      }
    });
  } catch (e) {
    //
  }
});

// definite state, where default response can be changed
app.use('/', (req, res) => {
  res.send({
    profile: {
      first_name: 'John',
      last_name: 'Pol'
    }
  });
});

app.listen(9090);
发布评论

评论列表(0)

  1. 暂无评论