方法

中英双语

您可以通过分配自定义函数来用自己的方法覆盖现有方法的签名和行为。

以下是覆盖 res.sendStatus 行为的示例。

app.response.sendStatus = function (statusCode, type, message) {
  // code is intentionally kept simple for demonstration purpose
  return this.contentType(type)
    .status(statusCode)
    .send(message)
}

上述实现完全改变了res.sendStatus的原始签名。它现在接受状态代码、编码类型和要发送到客户端的消息。

现在可以这样使用被覆盖的方法:

res.sendStatus(404, 'application/json', '{"error":"resource not found"}')