Serverless express with handlebars

Hello everyone!

It’s good to be part of this community. I’ve been using Netlify for a while now, but today I got stuck on some problems I will detail below. Any help is greatly appreciated.

What I’m trying to do: a serverless express app with handlebars view engine.
What happened?

  1. The redirects seem to be broken. I have this repo, which is a fork of this one. I’ve added some redirect rules in netlify.toml that do not work. The strangest thing is that the same redirect rules work just fine when i run netlify dev from my machine. My netlify instance is https://netlify-express-handlebars.netlify.com. The new route is /foo.

  2. On the “long” route, /.netlify/functions/server/foo, the view engine is not working. From the function logs I get this message: Error: Failed to lookup view "foo" in views directory "/var/task/views". This also works on my local machine, when I run netlify dev. Any ideas?

Have a wonderful day!
Victor

Hi @victorocna, are you zipping your view directory in with the rest of your function for deployment? Note that it works locally because all the files are there, but when you deploy your function you need to make sure all of the files you need are part of the functions deployment.

Hi @futuregerald! Great idea, I wasn’t doing it. Now I’m copying the views directory with copy-webpack-plugin. I’ve also set the express views directory where I figured the views would be copied.

app.set('views', path.join(__dirname, '/.netlify/functions/views'))

After I run netlify-lambda build express -c ./webpack.config.js, the functions directory where the app should be deployed has the following structure:

– views
---- foo.hbs
– server.js

I thought it would be enough, but I’m still not able to access the views.
Am I on the right track or did you mean something else by zipping the views? I’ve extensively searched for a similar example, but I could not find one.

Hi,

Yes, have you tried zipping the function yourself to make sure all the files are there and deploying that? that way we can eliminate the build function as a possible culprit while debugging the issue.