CodeGenieApp / CodeGenieApp/serverless-express

Unable to Stream Responses from AWS Lambda

Open
#655 8 comments 6 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
5.3k
Forks
674
PR merge metrics
No merged PRs in 30d

Description

I am facing difficulties in streaming responses from AWS Lambda. Initially, I attempted to use the serverless-http package for this purpose, but I learned that it doesn't support streaming. Subsequently, I switched to the @vendia/serverless-express package, hoping it would resolve the issue. However, even with this package, I can still not achieve streaming functionality.

My code:
```
const serverless = require("serverless-http"),
express = require("express"),
morgan = require("morgan"),
helmet = require("helmet"),
bodyParser = require("body-parser"),
awsServerlessExpress = require("@vendia/serverless-express");

const cors = require("cors");

const rootPrefix = ".",
coreConstants = require(rootPrefix + "/config/coreConstants"),
apiRoutes = require(rootPrefix + "/routes/index"),
setResponseHeader = require(rootPrefix + "/middlewares/setResponseHeader");

// Set worker process title.
process.title = "";

const app = express();

// Use Morgan middleware to log requests
app.use(morgan("combined"));

// Enable CORS
app.use(cors());

// Helmet can help protect the app from some well-known web vulnerabilities by setting HTTP headers appropriately.
app.use(helmet());

// Node.js body parsing middleware. Default limit is 100kb
app.use(bodyParser.json({ limit: "2mb" }));

// Parsing the URL-encoded data with the qs library (extended: true). Default limit is 100kb
app.use(bodyParser.urlencoded({ extended: true, limit: "2mb" }));

app.get("/stream", async (req, res) => {
res.setHeader("Content-Type", "text/plain");
for (let i = 0; i < 10; i++) {
res.write(`Line ${i}\n`);
await new Promise((resolve) => setTimeout(resolve, 1000));
}
res.end();
});

// Set response header to prevent response caching
app.use(setResponseHeader());

// If running in development mode, start the server on port 8080, else export handler for lambda
if (
coreConstants.ENVIRONMENT === "development" ||
coreConstants.ENVIRONMENT === "test"
) {
console.log("Server running on 8080");
app.listen(8080);
module.exports = { handler: app };
} else {
// Export the handler for Lambda on production
module.exports.handler = awsServerlessExpress({ app })
}
```

Contributor guide

Open the contributing guide

Research direction

Begin with the `/stream` route and the production `awsServerlessExpress({ app })` handler shown in the report; then inspect this repository’s Lambda and streaming support. Reproduce the delayed ten-line response and compare the local Express path with the Lambda path. Done should be a confirmed fix or a clear repository-supported explanation of whether streaming can work, backed by coverage or documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, express, javascript, node.js
Domain
api, backend, cloud
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.