Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node.js中Express 4.0中res.render回调参数的用途是什么?_Javascript_Node.js_Express_Pug - Fatal编程技术网

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

Javascript Node.js中Express 4.0中res.render回调参数的用途是什么?,javascript,node.js,express,pug,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.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) {
  // ...
});

我了解前两个参数的用途,但不了解最后一个参数。

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

从文件中:

// 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) {
  // ...
});
如果提供,该方法将返回可能的错误和呈现的字符串,但不执行自动响应


谢谢这是有道理的,在发送模板之前,应该先缩小模板。您是否有其他对使用此回调参数有用的修改案例?@roadtocode取决于您要执行的操作。。。你可以用它来做任何你需要的事情。例如,我使用了渲染的数据部分来发送HTML电子邮件(来自HBS渲染的正文)。发送电子邮件后,我将另一个JSON呈现为API响应。顺便问一下,有没有其他方法在没有REQ的情况下调用render?理想情况下,我希望在我的模型代码中呈现HBS,以获取电子邮件内容,而不是路由,