I have a simple Express app, hosted on AWS, using the Serverless framework.
I'm using serverless-http
to wrap the express app for deployment to an AWS lambda function, and express-async-await
to allow the use of async functions for routes.
For one of my routes I want to return a page to the user then continue performing various async tasks. Here is an abbreviated version of my code:
const serverless = require("serverless-http");
const express = require("express");
const mustacheExpress = require("mustache-express");
const aa = require("express-async-await");
// configure our express app
const app = aa(express());
app.use(express.static("static"));
app.engine("html", mustacheExpress());
app.set("view engine", "html");
app.set("views", __dirname + "/views");
app.get("/", async (request, response) => {
response.render("generating", { });
// various async tasks
await s3.putObject({ });
await s3.putObject({ });
});
module.exports.handler = serverless(app);
However, from inspecting the logs I can see that the tasks that follow the response.render
stop mid-way. The lambda returns without an error, no timeouts, but the tasks are stopped.
Moving response.render
to the end of the route means that the async tasks are executed, however, the user sees a spinner rather than a rendered HTML holding pages.
From reading around, I've found numerous references that indicate Express is more than happy to continue execution after a response has been returned.
Any ideas what is going wrong in my case?