dougmoscrop / serverless-http

Use your existing middleware framework (e.g. Express, Koa) in AWS Lambda 🎉
Other
1.72k stars 166 forks source link

Express View Engine Support #138

Open matt-peck opened 4 years ago

matt-peck commented 4 years ago

Does this support Express' ability to render view template files?

const express = require('express');
const path = require('path');
const serverless = require('serverless-http');
const app = express();

const router = express.Router();

router.get('/', (req, res) => {
  res.render('index', { name: 'John' });
});

app.engine('jsx', require('express-react-views').createEngine());
app.set('views', path.join(__dirname, "views"));
app.set('view engine', 'jsx');

app.use('/.netlify/functions/server', router);  // path must route to lambda
app.use('/', (req, res) => res.sendFile(path.join(__dirname, '../index.html')));

module.exports = app;
module.exports.handler = serverless(app);

When I run the express app locally, it works fine as expected. When I run it using the serverless wrapped version without attempting to res.render() and set express to use the view engine, it also works fine as expected.

However, the moment I attempt to use the view engine (and others) I started have path and directory issues and the app can't seem to find my views folder.

dougmoscrop commented 4 years ago

I have not tested this, is it possible something else during packaging is causing the views folder to be unavailable?

matt-peck commented 4 years ago

I feel like that is likely but I'm just a bit out of my depth. Can you point me in the right direction?

dougmoscrop commented 4 years ago

I guess the first step would be to run serverless package and then cd .serverless and unzip {xyz}.zip where xyz is the name of your service - I would look in the unzipped contents to see if they match.

matt-peck commented 4 years ago

Thanks!

dougmoscrop commented 4 years ago

How did you make out? Is this issue ok to close?