繁体   English   中英

Javascript / Node.js 导入 html 文件

[英]Javascript / Node.js importing html file

我正在制作一个 node.js 服务器,它可以按需发送电子邮件。 变量“输出”是我想通过 email 发送的。 当我使用内联 html 时,它工作正常,但是我想导入一个完整的 html 文件。

我尝试使用 <object> 和 <link> 导入与 app.js 位于同一文件夹中的“file.html”,但两者都显示为白页。

app.post("/send", (req, res) => {
    console.log("sending email...");
    const email = req.body.data.email;
    const customerId = req.body.data.customer_id;
    const orderId = req.body.data.order_id;

    //const output = `<h3>Hi, this works</h3>` // WORKS
    //const output = `<object type="text/html" data="file.html"></object>`; // Doesn't work
    const output = `<link href="file.html" rel="import" />`;                    // Doesn't work
;

非常感谢您的帮助。 如有必要,我可以提供更多代码。

将其插入 your.post 方法之外,以便在程序启动时运行一次并填充 output 而不是每次调用 your.post 或 .get 方法时都读取文件

const fs = require('fs');
const output = fs.readFileSync('/path/to/file.html').toString();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM