Node.js Express 4.0 中的 res.render 回调参数的用途是什么?

2024-05-16

目的是什么res.render回调参数?

在什么情况下,由于模板已被指定为第一个参数,因此人们会想要使用这样的回调参数?

这是文档中的代码:

// send the rendered view to the client
res.render('index');

// if a callback is specified, the rendered HTML string has to be sent explicitly
res.render('index', function(err, html) {
  res.send(html);
});

// pass a local variable to the view
res.render('user', { name: 'Tobi' }, function(err, html) {
  // ...
});

我理解前两个论点的目的,但不明白最后一个。


通过回调,您可以在发送模板之前拦截渲染的模板。在发送给客户端之前,您可能需要缩小它或以其他方式修改它。

从文档中:

如果提供,该方法将返回可能的错误和呈现的字符串,但不执行自动响应。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Node.js Express 4.0 中的 res.render 回调参数的用途是什么? 的相关文章

随机推荐