I have managed to get my Node.js Express app successfully deployed using serverless and serverless-http. However, whenever I access my endpoint URL, I get the response:
Cannot get /
I have my code setup to run in regular Express app server mode when running locally, and in serverless-http assisted mode when running as a Lambda function endpoint. The selection is controlled via an environment variable. When I run locally on my PC, the Express index page shows fine. When I run in Lambda endpoint mode, I get the error above. I have checked the CloudWatch logs for my Lambda function and there are no errors at all.
What could be the problem? Why is routing failing when my app is running as a Lambda function but works fine when running locally on my PC?
Below is my app.js code for the Lambda function execution context:
// We are running as a REST API endpoint in the cloud.
const serverless = require('serverless-http');
const express = require('express');
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.get('/api/info', (req, res) => {
res.send({ application: 'my-node-js-express-app', version: '1' });
});
app.post('/api/v1/getback', (req, res) => {
res.send({ ...req.body });
});
// This is the entry point specified in the serverless.yml file for our
// REST API we expose to provide the web server content.
module.exports.handler = serverless(app);
console.log(errPrefix + `Running in Lambda function execution context.`);