0

I'm making a node.js server which sends emails on demand. The variable "output" is what I want to send via email. When I use inline html it works fine, however I want to import a complete html file instead.

I've tried using <object> and <link> to import "file.html" which is in the same folder as app.js, but both show up as a white page.

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
;

Help is much appreciated. I can supply more code if necessary.

2
  • Read the file into memory and store it in a variable. The html inside your output variable is not parsedd which is why your file is never included. Commented Dec 8, 2020 at 13:58
  • @t348575 Oh makes sense, I'm not that fluent in Node.js yet, could you show how to do that or point me in the right direction? Would I use require('..') for that? Commented Dec 8, 2020 at 14:01

1 Answer 1

2

Insert this outside your .post method so that it is run once when your program starts and output is populated instead of reading the file every time your .post or .get method is called

const fs = require('fs');
const output = fs.readFileSync('/path/to/file.html').toString();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.