Serverless express with handlebars

That is correct.

If you use netlify/functions there is no need to specify the functions directory as this is the default. In this instance you would have

[functions]
  [functions.post] # assuming the functions is called post.js
    included_files = ["posts/*.md"] # assuming this is where include files are

You can specific the functions directory in the UI also. If you add the directory to the netlify.toml this will override the value set in the UI.

1 Like

@coelmay this is exactly what I’m looking for, but I’m still running into issues with the ENOENT error. I’m pretty sure it’s something silly, but I’m new to this so I’m at a bit of a loss for troubleshooting.

Try changing

 const data = fs.readFileSync('./template.hbs', 'utf8');

to

 const data = fs.readFileSync('./templates/template.hbs', 'utf8');

I suggest leaving the styles.css file in the site root, and linking to it instead e.g.

<link rel="stylesheet" href="/styles.css" />
1 Like

You completely rock!! That did the trick. I’m curious about the css file being in the site root… so eventually I need to have 3 functions (1 per template). My original plan was to structure it like this:

site/
  index.html
functions/
  template1/
    template1.js
  template2/
    template2.js
templates/
  template1/
    template1.hbs
    template1.css
  template2/
    template2.hbs
    template2.css

What you’re recommending would be to put the 3 css files under site/ but why is that?

The templates directory is not accessible outside the function, so any files referenced in it (e.g. via <link href="">, or <img src="">) aren’t accessible once the function finished rendering and returns the content.

Your directory structure is a little overly complex. Simplify

.
β”œβ”€β”€ functions
β”‚   └── template.js
β”œβ”€β”€ templates
β”‚   β”œβ”€β”€ template1.hbs
β”‚   β”œβ”€β”€ template2.hbs
β”‚   └── template3.hbs
└── site
    β”œβ”€β”€ template1.css
    β”œβ”€β”€ template2.css
    └── template3.css

With the included_files use a wildcard e.g.

included_files = ["templates/*.hbs"]

You could have three separate functions and use static routing to specific paths to a function, or have a single function that determines which template to load based on the path.

1 Like

All that makes sense, thanks again for the help!