The nodeJS / Express code below enables us to to detect whether the user closes their connection before response.end();
is called. I am trying to recreate something similar in AWS Lambda.
Is there any way of using the context object passed to the Lambda handler to achieve this?
I'm also looking at serverless-express but would prefer to avoid using an external library, if possible.
const express = require('express');
const app = express();
app.get('/', function (req, res) {
res.setHeader('Content-Type', 'text/html; charset=utf-8');
let count = 0;
let gracefulClose = false;
req.on("close", function () {
if (!gracefulClose) {
console.log('Connection closed before res.end() called');
}
});
req.on("end", function () {
gracefulClose = true;
console.log('res.end() called');
});
setInterval(() => {
if (count === 3) {
res.end();
} else {
count = 1;
}
}, 1000);
});
app.listen(3000);
CodePudding user response:
Unfortunately, this is not possible. Lambda functions are always invoked through the 'invoke' API. This might be via an Application Load Balancer or an API Gateway, but it's always the case. Lambda functions don't know about the client connection and don't know if it's been closed early.