'In AWS Lambda using Node, can't get Express to work with aws-serverless-express
Below is my sample code that is not working. No matter what I set up, I can't get Express to render my ejs page.
Note: I have an API Gateway in front of the Lambda. That will only let in the traffic I want. Everything that hits my Lambda should have the express page rendered. While not in the code I am showing below, once working, I will do some validation and lookups before using express to show the page.
index.js
"use strict";
const AWS = require('aws-sdk');
AWS.config.region = 'us-east-1';
const awsServerlessExpress = require('aws-serverless-express');
const app = require('./displayRouter.js');
const serverDisplay = awsServerlessExpress.createServer(app.router)
exports.handler = async (event, context, callback) =>{
try
{
console.log("testing");
app.setValues("setting values called correctly");
console.log("before awsserverless")
await awsServerlessExpress.proxy(serverDisplay, event, context);
console.log("after call to awsserverless");
}catch(e){
console.log("ALERT: 500 error " + e);
}
}
displayRouter.js
const express = require('express');
const path = require('path');
const router = express();
router.set('view engine', 'ejs');
const setValues = async (testValueIn) =>
{
console.log("setting Values IN ROUTER");
console.log("value sent i: " + testValueIn);
}
router.set('views', express.static(path.join(__dirname, 'views')));
router.all('/', (req, res) => {
console.log('inside function before diaplying page');
res.render("emailTestPage",{ });
});
module.exports = {router, setValues};
Note, I call setValues to prove that I am reaching the page correctly. You can see that in the output below. But when I call router.all, it is never actually hit. I have tried router.use('/'...., router.get('/'..., router.post('/', ... None of them are every hit.
Output in Lamda log:
2022-03-12T17:41:41.567-05:00 2022-03-12T22:41:41.549Z ggea5c27-4955-42c0-baf9-501e3f071bc7 INFO testing
2022-03-12T17:41:41.568-05:00 2022-03-12T22:41:41.567Z ggea5c27-4955-42c0-baf9-501e3f071bc7 INFO setting VALUES IN ROUTER
2022-03-12T17:41:41.568-05:00 2022-03-12T22:41:41.568Z ggea5c27-4955-42c0-baf9-501e3f071bc7 INFO value sent i: setting values called correctly
2022-03-12T17:41:41.568-05:00 2022-03-12T22:41:41.568Z ggea5c27-4955-42c0-baf9-501e3f071bc7 INFO before awsserverless
2022-03-12T17:41:41.607-05:00 2022-03-12T22:41:41.570Z ggea5c27-4955-42c0-baf9-501e3f071bc7 INFO after call to awsserverless
2022-03-12T17:41:41.669-05:00 END RequestId: ggea5c27-4955-42c0-baf9-501e3f071bc7
2022-03-12T17:41:41.669-05:00 REPORT RequestId: ggea5c27-4955-42c0-baf9-501e3f071bc7 Duration: 160.20 ms Billed Duration: 161 ms Memory Size:
EDIT******** I have dumbed this down to one page. It is still not working.
"use strict";
const AWS = require('aws-sdk');
AWS.config.region = 'us-east-1';
const express = require('express');
const app = express();
app.set('view engine', 'ejs');
const awsServerlessExpress = require('aws-serverless-express');
app.get("/", (req, res) => {
console.log('inside app');
res.render("testPage",{ });
});
exports.handler = async (event, context, callback) =>{
try
{
console.log("CREATING SERVER");
let server = awsServerlessExpress.createServer(app);
await awsServerlessExpress.proxy(server, event, context);
console.log("after call to awsserverless");
}catch(e){
console.log("ALERT: 500 error " + e);
}
}
and here is the output
2022-03-14T13:29:02.142-04:00 2022-03-14T17:29:02.124Z ddde0b20-2700-470a-bcff-af81aec74f61 INFO CREATING SERVER
2022-03-14T13:29:02.223-04:00 2022-03-14T17:29:02.185Z ddde0b20-2700-470a-bcff-af81aec74f61 INFO after call to awsserverless
2022-03-14T13:29:02.286-04:00 END RequestId: ddde0b20-2700-470a-bcff-af81aec74f61
2022-03-14T13:29:02.286-04:00 REPORT RequestId: ddde0b20-2700-470a-bcff-af81aec74f61 Duration: 191.47 ms Billed Duration: 192 ms Memory Size
Solution 1:[1]
I figured it out. I wasted so much time on such a simple mistake. I needed to add "return" to the awsServerlessExpress.proxy line.
return awsServerlessExpress.proxy(server, event, context,'PROMISE').promise;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andy P |
