-1

I want to turn every res.render to res.json for the whole application. The reason I'm doing so is to avoid bad code as only in development environment, I would like to return JSON rather than HTML.

I found this question and following the solution I thought it would work with:

const renderToJson = function(req, res, next) {
  res.render = function(view, options, callback) {
    res.status(200).json(res, view, options, callback);
  };
};
app.use(renderToJson);

Or

const renderToJson = function(req, res, next) {
  const _render = res.status(200).json;
  res.render = function(view, options, callback) {
    _render.call(res, view, options, callback);
  };
};
app.use(renderToJson);

But unfortunately it didn't. I continue to receive HTML with no errors.

Curcuma_
  • 851
  • 2
  • 12
  • 37

1 Answers1

0

I could obtain what I wanted with

const renderToJson = function(req, res, next) {
  let _render = res.render;
  res.render = function(view, options, callback) {
    this.json(options);
  };
  next();
};
app.use(renderToJson);

Keeping my response not approved as there might be drawbacks for this.

Curcuma_
  • 851
  • 2
  • 12
  • 37