Closed bzzanui closed 2 years ago
Finally I have fixed this issue by myself. as AWS ALB is expecting single value in headers object which means you could not pass string[] to the value Also I could not see much function being implemented to support the full conversion from Class Http.ServerResponse https://nodejs.org/api/http.html#class-httpserverresponse To AWS request response Object: https://docs.aws.amazon.com/lambda/latest/dg/services-alb.html
{
"statusCode": 200,
"statusDescription": "200 OK",
"isBase64Encoded": False,
"headers": {
"Content-Type": "text/html"
},
"body": "<h1>Hello from Lambda!</h1>"
}
Lucky you still allow us to do the manually mapping ourself
const sls = require('serverless-http');
module.exports.handler = sls(server, {
binary: binaryMimeTypes,
request(request, event, context) {
request.context = event.requestContext;
request.body = event.body;
},
async response(response, event, context) {
return new Promise(resolve => {
response.headers['Access-Control-Allow-Credentials'] = true;
const cookies = response.getHeader('set-cookie');
if (cookies && cookies.length > 0) {
console.log('cookies', cookies);
response.cookies = cookies;
//set the first cookie
response.headers['set-cookie'] = cookies[0];
}
resolve();
})
}
});
Was hoping this library 0 config and use directly but still need to change a bit to do the transformation, this method works in ALB without multipleHeaderValue enable, as once enabled the headers field will be ignore then some page Content-Type will be incorrect, which means you need to do more mapping to copy all headers to multiple headers yourself. But all in all this library still works.
Hope this helps
Hope some one could help, recently working the project and when turning on the "Multi value headers" in the ALB target group the cookie is sending correctly but the HTML page headers are also mess up that HTML page become download page.
Here is server.js start page:
I have to override the body as when using in ALB the body is not actually sending to NextJS API pages. Try to solve the cookie issue, but looks like there is some gap that Serverless HTTP might not support NextJS+ALB well
"serverless-http": "^2.7.0", "next": "^12.0.1", "next-pwa": "^5.4.0",
Thanks.