roa*_*ode 5 javascript node.js express pug
回调参数的目的是什么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) {
// ...
});
Run Code Online (Sandbox Code Playgroud)
我理解前两个论点的目的,但不明白最后一个。
通过回调,您可以在发送模板之前拦截渲染的模板。在发送给客户端之前,您可能需要缩小它或以其他方式修改它。
从文档中:
如果提供,该方法将返回可能的错误和呈现的字符串,但不执行自动响应。
| 归档时间: |
|
| 查看次数: |
5020 次 |
| 最近记录: |