Node.js 在mongo中使用dust进行服务器端渲染

Node.js 在mongo中使用dust进行服务器端渲染,node.js,mongodb,dust.js,Node.js,Mongodb,Dust.js,是否可以从MongoDB获取数据,并在服务器端本身为node js项目呈现html模板 到目前为止,在我的服务器端js文件中,我已经完成了以下工作 //Failing array will be populated by a db.find later on. var failing = [ { name: "Pop" }, { name: "BOB" } ]; /*Now i have to send a mail from the server for which I'm

是否可以从MongoDB获取数据,并在服务器端本身为node js项目呈现html模板

到目前为止,在我的服务器端js文件中,我已经完成了以下工作

//Failing array will be populated by a db.find later on.
var failing = [
    { name: "Pop" }, 
    { name: "BOB" }
];

/*Now i have to send a mail from the server for which I'm using nodemailer.
 Where do i store the template ?  This is what I've done in the same file */

var template = "<body>{#failing} <p>{.name}</p> {/failing}</body>"

// Add this as the body of the mail and send it.

//失败的数组将在稍后由db.find填充。
var失败=[
{name:“Pop”},
{name:“BOB”}
];
/*现在我必须从使用nodemailer的服务器发送邮件。
我在哪里存储模板?这就是我在同一个文件中所做的*/
var template=“{#failing}{.name}

{/failing}” //将此添加为邮件正文并发送。

我不知道如何渲染数据以及如何显示数据。我知道将模板存储在变量中是不对的,但我不确定还能做什么。

如果模板很短,可以将其存储在变量中而不会出现问题。显然,您也可以将其存储在文件中

假设您决定将其存储在一个文件
index.dust

<body>{#failing} <p>{.name}</p> {/failing}</body>
{#failing}{.name}

{/failing}
现在,在节点控制器中,您需要加载文件并从中生成html内容:

const fs = require('fs');
const dust = require('dustjs-linkedin');

// Read the template
var src = fs.readFileSync('<rest_of_path>/index.dust', 'utf8');

// Compile and load it. Note that we give it the index name.
var compiled = dust.compile(src, 'index');
dust.loadSource(compiled);

// Render the template with the context. Take into account that this is
// an async function
dust.render('index', { failing: failing }, function(err, html) {
    // In html you have the generated html.
    console.log(html);
});
const fs=require('fs');
const dust=require('dustjs-linkedin');
//阅读模板
var src=fs.readFileSync('/index.dust',utf8');
//编译并加载它。注意,我们给它起了索引名。
var compiled=dust.compile(src,“index”);
粉尘.负荷源(已编译);
//使用上下文呈现模板。考虑到这是
//异步函数
render('index',{failing:failing},函数(err,html){
//在html中,您拥有生成的html。
log(html);
});

选中以避免每次使用模板时都必须编译模板。

如果模板太短,则可以将其存储在变量中而不会出现问题。显然,您也可以将其存储在文件中

假设您决定将其存储在一个文件
index.dust

<body>{#failing} <p>{.name}</p> {/failing}</body>
{#failing}{.name}

{/failing}
现在,在节点控制器中,您需要加载文件并从中生成html内容:

const fs = require('fs');
const dust = require('dustjs-linkedin');

// Read the template
var src = fs.readFileSync('<rest_of_path>/index.dust', 'utf8');

// Compile and load it. Note that we give it the index name.
var compiled = dust.compile(src, 'index');
dust.loadSource(compiled);

// Render the template with the context. Take into account that this is
// an async function
dust.render('index', { failing: failing }, function(err, html) {
    // In html you have the generated html.
    console.log(html);
});
const fs=require('fs');
const dust=require('dustjs-linkedin');
//阅读模板
var src=fs.readFileSync('/index.dust',utf8');
//编译并加载它。注意,我们给它起了索引名。
var compiled=dust.compile(src,“index”);
粉尘.负荷源(已编译);
//使用上下文呈现模板。考虑到这是
//异步函数
render('index',{failing:failing},函数(err,html){
//在html中,您拥有生成的html。
log(html);
});
检查以避免每次必须使用模板时都编译模板