写一个文件的“ hapijs”响应

时间:2018-08-03 09:56:41

标签: node.js hapijs

目前,我正在执行类似操作,以便将文件提供给浏览器

handler: function (request, h) {

    // get data here based on the request
    data = getData(…);

    return h.view(
        template,
        data,
        { layout: layout }
    );
}

但是,我想改为将响应写到文件中(这可能是我制造另一种静态站点生成器的错误尝试,比其他任何东西都更多地学习编程)。我该怎么办?

更新:好的,就像用以下内容替换return h.view(…)部分一样

const template = handlebars.compile(
    fs.readFileSync('views/template.html', 'utf8')
);
const layout = handlebars.compile(
    fs.readFileSync('views/layout/main.html', 'utf8')
);

fs.writeFileSync(`public/projects/${file}`, template(entryData));
return 'done';

那行得通。但是注意上面的layout位吗?如何正确使用所有部分布局?

更新2:弄清楚了

    const template = handlebars.compile(
        fs.readFileSync('views/template.html', 'utf8')
    );

    handlebars.registerPartial({
        content: fs.readFileSync('views/partials/content.html', 'utf8'),
        footer: fs.readFileSync('views/partials/footer.html', 'utf8'),
        header: fs.readFileSync('views/partials/header.html', 'utf8'),
        meta: fs.readFileSync('views/partials/meta.html', 'utf8'),
        toc: fs.readFileSync('views/partials/toc.html', 'utf8')
    });

    const layout = handlebars.compile(
        fs.readFileSync('views/layouts/main.html', 'utf8')
    );



    fs.writeFileSync(`public/projects/${file}`, layout(entryData));

上面的技巧。谢谢大家的帮助:)

0 个答案:

没有答案