CodeGenieApp / CodeGenieApp/serverless-express

express.static doesnt work with ALB endpoint

Open
#563 11 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
5.3k
Forks
676
PR merge metrics
No merged PRs in 30d

Description

When 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);
});
```

Contributor guide

Open the contributing guide

Research direction

No source file or test is named. Start by reproducing the express.static robots.txt request through the ALB, then compare it with the standalone Express and serverless offline cases described in the issue. Done means static files, including robots.txt, load through the ALB without a 502 while missing files still return 404.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, express, javascript
Domain
api, backend, cloud
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.