【问题标题】:express create standard JSON return formatexpress 创建标准 JSON 返回格式
【发布时间】:2021-08-06 14:14:54
【问题描述】:

我是新来的表达者,目前如果我想发送回复,我会执行以下操作

//...
res.json({
  status: 200,
  message: "success",
  data: "something"
})
//...

对于我所有的控制器功能,我都会这样做。我想这有点压倒性,所以我在想是否有可能做出一个普遍的回应。

所以经过一番搜索,我发现有两种方法可以实现这一点:

1。使用在每条路由结束时运行的中间件

我发现了这个answer。这很清楚,但我认为这是浪费时间,然后我的控制器将不够可读,我必须在每个控制器功能中执行以下操作

req.responseObject = response;
return next()

2。覆盖快递本身的响应类

这很有希望,但不幸的是,经过多次尝试并遵循快速文档后,我无法让它工作 这是我迄今为止尝试过的

我在这里创建了这个 util 函数:

//myRes.js
module.exports = function (code, message, payload) {
  return this.status(code).json({
    status: 'success',  // this will be variable according to whatever code I get
    code: code,
    message: message || '',
    payload: payload || '',
  });
};

然后我在 app.js 中添加了这一行

// app.js
const express = require('express');
const app = express();
const myRes= require('./utils/myRes');
app.response.send = myRes;
//...

这就是我在控制器上使用它的方式

res.send(200, "hello world", [{"key": "value"}])

当我尝试运行它时,我得到了这个错误堆栈

RangeError: Maximum call stack size exceeded
    at JSON.stringify (<anonymous>)
    at stringify ([project_dir]\node_modules\express\lib\response.js:1123:12)
    at ServerResponse.json ([project_dir]\node_modules\express\lib\response.js:260:14)
    at ServerResponse.module.exports [as send] ([project_dir]\src\utils\response.js:2:28)
    at ServerResponse.json ([project_dir]\node_modules\express\lib\response.js:267:15)
    at ServerResponse.module.exports [as send] ([project_dir]\src\utils\response.js:2:28)
    at ServerResponse.json ([project_dir]\node_modules\express\lib\response.js:267:15)
    at ServerResponse.module.exports [as send] ([project_dir]\src\utils\response.js:2:28)
    at ServerResponse.json ([project_dir]\node_modules\express\lib\response.js:267:15)
    at ServerResponse.module.exports [as send] ([project_dir]\src\utils\response.js:2:28)
    at ServerResponse.json ([project_dir]\node_modules\express\lib\response.js:267:15)
    at ServerResponse.module.exports [as send] ([project_dir]\src\utils\response.js:2:28)
    at ServerResponse.json ([project_dir]\node_modules\express\lib\response.js:267:15)
    at ServerResponse.module.exports [as send] ([project_dir]\src\utils\response.js:2:28)
    at ServerResponse.json ([project_dir]\node_modules\express\lib\response.js:267:15)
    at ServerResponse.module.exports [as send] ([project_dir]\src\utils\response.js:2:28)

为什么 JSON.stringify 抱怨我没有在我的整个应用程序中使用这个函数

我不仅限于这两种解决方案,如果您认为更好,您可以提出其他方法

【问题讨论】:

  • 这本身不是您问题的答案,但您的回复中不需要“状态”和“代码”,这应该由协议处理。阅读codecademy.com/articles/what-is-rest。通常成功返回状态码 200 就足够了。
  • 这只是我想要做的一个例子
  • 好的,问题可能是“.json”实际上调用了“.send”,使其成为递归调用。 github.com/expressjs/express/blob/…
  • 尝试修改“app.response.send = myRes;”到“app.response.customSend = myRes;”并改为调用该函数
  • 哇,原来如此。感谢您的帮助

标签: javascript node.js json express


【解决方案1】:

澄清@Ariel 在 cmets 中所说的内容

Express 响应调用在内部发送在 [120、123、136] 行从 express 引用此 function,这导致了无限循环,这就是它所说的原因

超过最大调用堆栈大小

所以我需要做的就是将我的函数名称从发送更改为“发送”,这样就可以了

尝试覆盖内置发送函数是个坏主意

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    相关资源
    最近更新 更多