I have this piece of code inside a controller:
exports.getintroDetails = async (req, res) => {
try {
const details = await IntroModel.find();
return res.render("final", { details , user:req.user });
} catch (error) {
console.log(error.message);
}
};
This code, as expected, renders the final.ejs file with the given data.
But instead, I want to show raw html which we get after rendering final.ejs with the given data. Is there any way to store the raw html as a string instead of rendering it?
So, instead of:
return res.render("final", { details , user:req.user });
I want something like this:
return res.send( view-source( res.render("final", { details , user:req.user }) ) );
Is there any function like view-source()
as described above?
CodePudding user response:
According to docs you can provide a callback
which will be executed with the rendered html
. When the callback is provided the response won't automatically be made.
exports.getintroDetails = async (req, res) => {
try {
const details = await IntroModel.find();
return res.render("final", { details , user:req.user }, (err, html) => {
console.log('rendered html!', html)
// now, the response has to be made manually.
res.status(200).send(html)
});
} catch (error) {
console.log(error.message);
}
};
It is also possible, to render it using app.render()
for only receiving the html
.
From the docs
Think of app.render() as a utility function for generating rendered view strings. Internally res.render() uses app.render() to render views.
app.render('final', function (err, html) {
// ...
})