Node.js中Express 4.0中res.render回调参数的用途是什么? [英] What is the purpose of the res.render callback argument in Express 4.0 in Node.js?

查看:288
本文介绍了Node.js中Express 4.0中res.render回调参数的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What is the purpose of the res.render callback argument?

在哪种情况下,由于已经将模板指定为第一个参数,所以要使用这样的回调参数?

In which case would one want to use such a callback argument, since a template is already assigned as the first argument?

这是文档中的代码:

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

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

I understand the purpose of the first two arguments, but not the last.

推荐答案

通过回调,您可以在继续发送渲染模板之前对其进行拦截.在发送给客户端之前,您可能希望将其缩小或对其进行修改.

With the callback, you can intercept the rendered template before sending it on. You might want to minify it or otherwise modify it before sending to the client.

从文档中:

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

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

这篇关于Node.js中Express 4.0中res.render回调参数的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆