Reputation: 3488
I'm using node.js, express, ejs and nodemail. I'm able to send mails with custom template if the template 'mail.ejs' is in the same folder as the controller 'contact.ejs' like this:
// Folder structure
controllers
contact.js
mail.ejs
// contact.js
const nodemailer = require('nodemailer');
const ejs = require("ejs");
...
const output = await ejs.renderFile(__dirname + "/mail.ejs", {
test: 'Test'
});
...
It works fine ... but now I want to have the template in a different folder, but I'm struggling to call/access it. How should the ejs.renderFile(?) look like to have this folder structure:
// Desired folder structure
controllers
contact.js
views
mail.ejs
// contact.js
const nodemailer = require('nodemailer');
const ejs = require("ejs");
...
const output = await ejs.renderFile(??????, {
test: 'Test'
});
...
Upvotes: 0
Views: 1270
Reputation: 3001
__dirname
tells you the absolute path of the directory containing the currently executing file.
By default, EJS is going to look for the file relative to process.cwd()
, the directory where the Node.js process was started. __dirname
is the directory that your JS file ( contact.js
) is in. If you don't want to join to __dirname
, you need to make your paths relative to where the Node.js process is started. Which means,
If folder structure is,
Root
server.js <--- EJS look for relative paths from here
views
-mail.ejs
And,
const output = await ejs.renderFile("/views/mail.ejs", {
test: 'Test'
});
Upvotes: 2