gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:05:21 25 4
gpt4 key购买 nike

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) {
// ...
});

我理解前两个参数的目的,但不理解最后一个。

最佳答案

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

来自文档:

If provided, the method returns both the possible error and rendered string, but does not perform an automated response.

关于javascript - Node.js Express 4.0 中 res.render 回调参数的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44379563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com