我想实现一个中间件,当pretty_ouput查询中存在时,它会在响应中返回格式化的 json。我尝试使用以下代码:
  // This code does not work as expected.
  app.use(function(req, res, next) {
    // Querystring `pretty_output` should prettify json output.
    if (req.query['pretty_ouput']) {
      app.set('json spaces', 2); // I'd like to set this only for this one request.
    }
    next();
  });但是,没有查询字符串的后续请求pretty_ouput也会被格式化。
json spaces有没有办法只为一个请求设置设置?理想情况下,我可以调用res.set('json spaces', 2)只为这一个响应设置此选项。我认为这对任何 Express 选项都很有用。