I'm trying to embed an EJS template named 'ui.ejs' into handler.js. The aim is to capture URL query parameters, then pass them to a function name 'ui.js' to capture data, that data is then passed onto the EJS UI template called 'ui.ejs'. But I keep getting this error:
Failure: ENOENT: no such file or directory, open 'D:\Studio\Work\YellowCard\dynamo_serverless_rest\.webpack\service/views/ui.ejs'
Seems webpack is interfering with my directory paths. The directory is under '/views/ui.ejs' NOT '.webpack\service/views/ui.ejs' like the error claims. How do I fix it? Thanks in advance..!
Here's the handler.js code:
// Import modules and dependencies
import {ui} from './index.js';
var ejs = require('ejs');
var fs = require('fs');
// Implementing the UI
export async function UserInterface(event, context, callback) {
// Capture data event parameters
const e = event.queryStringParameters;
// Get UI parameters
let params = await ui(e);
var htmlContent = fs.readFileSync(__dirname + '/views/' + 'ui.ejs', 'utf8');
var template = ejs.compile(htmlContent);
return {
statusCode: 200,
headers: { 'Content-type': 'text/html' },
body: JSON.stringify(template(params)),
};
};
You can use copy-webpack-plugin to copy the views folder to the destination directory.
In your webpack configuration file (webpack.config.js
)
const CopyPlugin = require("copy-webpack-plugin");
// ...
plugins: [
new CopyPlugin({
patterns: [
{ from: "views", to: path.join(__dirname, '.webpack', 'service', 'views') },
],
}),
],
And also update serverless.yml file to include views directory to your lambda function
package:
include:
- views/**