How to render a view to a string?

狂风中的少年 提交于 2019-11-29 16:31:06

Express' res.render() is still accessible in your res object.

function registerAction(req, res) {
   // handle user registration

  res.render('/path/to/view', function (err, html) {
    // email user
    sendEmail({
        to: newUser.email,
        subject: "Welcome",
        text: html
    });
  }

  // render view to the user
  return res.view({
     user: newUser
  });
}

To improve a little on crzrcn's answer, to ensure

function registerAction(req, res) {
   // handle user registration

  // don't include the .ejs of the end of your view, this assumes a file in path/to/view.ejs
  res.render('path/to/view', function (err, html) {
    // email user
    sendEmail({
        to: newUser.email,
        subject: "Welcome",
        html: html // rather than text in crzrcn's answer
    });
  }

  // render view to the user
  return res.view({
     user: newUser
  });
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!