CodeGenieApp / CodeGenieApp/serverless-express
Unable to Stream Responses from AWS Lambda
- Ngôn ngữ chính
- JavaScript
- Star
- 5.3k
- Fork
- 676
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
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 })
}
```
Hướng dẫn đóng góp
Hướng nghiên cứu
Bắt đầu với route `/stream` và handler production `awsServerlessExpress({ app })` được nêu trong báo cáo; sau đó kiểm tra phần hỗ trợ Lambda và streaming của repository này. Tái hiện response bị trì hoãn gồm mười dòng và so sánh đường đi Express cục bộ với đường đi Lambda. Công việc được xem là hoàn tất khi có bản sửa được xác nhận hoặc một giải thích rõ ràng, được repository hỗ trợ, về việc streaming có thể hoạt động hay không, dựa trên coverage hoặc tài liệu.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- aws, express, javascript, node.js
- Lĩnh vực
- api, backend, cloud
- Loại issue
- Lỗi
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Cần làm rõ
- Mức phù hợp với người mới
- 25/100