express.static doesnt work with ALB endpoint
See original GitHub issueWhen using express.static, the files within the static path do not load and the endpoint (Application Load Balancer) returns a 502.
app.use(express.static(path.resolve("./public")));
I was trying to add a robots.txt to the static path and let express.static handle the routing and response, but when I try to load the page, it responds with a 502 Bad Gateway. I’m 100% sure the file exists and the static path is correct, I’ve logged it. Also, when I try to load an asset that isnt in the static path, like /robo.txt, it responds with a 404, so it’s recognizing something.
I’ve looked into what can cause a 502 from ALB, some docs on AWS says that either the response is larger than 1MB or the lambda timed out, which neither of these things is the case in my situation. My speculation is maybe the response from express.static isnt being handled correctly for an ALB in “vendia/serverless-express”? Possibly a content-type header issue? I’ve run the app locally via serverless offline and as a stand alone express server, in both cases, the /robots.txt loads fine, which is why I think it might have something to do with how ALB is integrated with the library.
Anyways, I worked around this issue by just explicitly setting the robots.txt path and importing the txt file
import txt from "../../robots.txt";
app.use("/robots.txt", (req, res, next) => {
res.set("Content-Type", "text/plain");
res.send(txt);
});
Issue Analytics
- State:
- Created a year ago
- Comments:11 (5 by maintainers)
Top GitHub Comments
I met a very similar issue and in my case it was a type of content-length header. express.static() produces content-length as a number, but apparently ALB requires headers as strings. As a workaround I have built wrapper around the lambda handler to stringify all headers.
yep, that worked! Idk if it’d be better for vendia/serverless-express to stringify all the headers by default or allow a custom function to be passed in to edit the headers, but at the very least mentioning it in the docs.
wrapper