Javascript/Node.js导入html文件

Javascript/Node.js导入html文件,javascript,node.js,nodemailer,Javascript,Node.js,Nodemailer,我正在制作一个node.js服务器,可以按需发送电子邮件。变量“output”是我想通过电子邮件发送的内容。当我使用内联html时,它工作得很好,但是我想导入一个完整的html文件 我尝试使用和导入“file.html”,它与app.js位于同一文件夹中,但都显示为白色页面 app.post("/send", (req, res) => { console.log("sending email..."); const email =

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

我尝试使用和导入“file.html”,它与app.js位于同一文件夹中,但都显示为白色页面

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
;
app.post(“/send”,(请求、回复)=>{
日志(“发送电子邮件…”);
const email=req.body.data.email;
const customerId=req.body.data.customer\u id;
const orderId=req.body.data.order\u id;
//const output=`嗨,这行得通`//行得通
//常量输出=``;//不工作
常量输出=``;//不工作
;

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

在.post方法之外插入此代码,以便在程序启动并填充输出时运行一次,而不是在每次调用.post或.get方法时读取文件

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

将文件读入内存并将其存储在变量中。输出变量中的html未被解析,这就是文件从未包含在内的原因。@t348575哦,这很有意义,我对Node.js还不是很熟练,你能告诉我怎么做,或者告诉我正确的方向吗?我会使用require(“..”)吗?