简体   繁体   中英

How to use an HTML templating engine with a Node.js function in an AWS lambda?

I have a lambda function that is supposed to return an HTML page.

This page needs a number of dynamic variables as well as custom stylesheets to function properly.

I have tried the regular way of returning HTML as a string, setting the correct content-type and substituting the dynamic variables I need within that string. This works.

However, I want to use a templating engine rather than writing out strings of HTML as Javascript.

I've tried using nunjucks but it's unable to find my HTML file since the build folder only has an app.js in it.

Assuming all your files are part of the bundle being uploaded to AWS, here's how I fixed the issue of the HTML file not being found:

The output of my esbuild is just a single app.js file and this is what was originally being pushed to AWS. I had to add a cp command to my build phase which copies all my.html files from my working directory to the build directory.

Once this is done, you can simply refer to your.html files using a combination of path and __dirname and Nunjucks (or any other templating engine) should be able to find your files without any issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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