question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

express.static doesnt work with ALB endpoint

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
Wafelcommented, Nov 15, 2022

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.

1reaction
ml27299commented, Nov 18, 2022

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

const serverlessExpress = require("@vendia/serverless-express");
let serverlessExpressInstance;

export const handler = async (event, context) => {
	function stringifyHeader(response) {
		for (let key in response.headers) {
			response.headers[key] = response.headers[key].toString();
		}
		return response;
	}

	if (serverlessExpressInstance) {
		const response = await serverlessExpressInstance(event, context);
		return stringifyHeader(response);
	}

	serverlessExpressInstance = serverlessExpress({
		app
	});

	const response = await serverlessExpressInstance(event, context);
	return stringifyHeader(response);
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Serve Static Files on a Dynamic Route using Express
I use below code to serve the same static files requested by different urls: ... Documentation states that dynamic routes with app.use() works....
Read more >
Issues · vendia/serverless-express - GitHub
Run Node.js web applications and APIs using existing application frameworks on AWS ... express.static doesnt work with ALB endpoint.
Read more >
Serving static files - AWS Elastic Beanstalk
Configure static files using the console · Open the Elastic Beanstalk console , and in the Regions list, select your AWS Region. ·...
Read more >
Serving static files in Express
To serve static files such as images, CSS files, and JavaScript files, use the express.static built-in middleware function in Express.
Read more >
Configure ASP.NET Core to work with proxy servers and load ...
Learn about configuration for apps hosted behind proxy servers and load balancers, which often obscure important request information.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found