Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It works with Application Load Balancer! #241

Closed
nabilfreeman opened this issue Jul 1, 2019 · 2 comments
Closed

It works with Application Load Balancer! #241

nabilfreeman opened this issue Jul 1, 2019 · 2 comments

Comments

@nabilfreeman
Copy link

Hey there, I was interested in trying out ALB instead of API Gateway for performance reasons.

I managed to get it working with the following code:

const serverless = (event, context) => {
	const app = Express();

	const binaryMimeTypes = [
		'application/json',
	];
	const server = AWSServerlessExpress.createServer(app, null, binaryMimeTypes);

	// Sometimes we receive encoded stuff in the url.
	// So we need to decode this to get Express parsing things properly.
	// We used to run this through Elastic Beanstalk and then API Gateway, and they decoded it for us.
	// Now we are using more custom stuff and DIY is required.

	// this is the home for our parsed query data.
	const query = {};

	// merge the raw query into one object.
	// it can come from different sources depending on API Gateway or Application Load Balancer.
	const raw_query = {
		...(event.query || {}),
		...(event.queryStringParameters || {}),
	};

	// decode everything back into utf-8 text.
	for (const key in raw_query) {
		const formatted_key = decodeURIComponent(key);

		query[formatted_key] = decodeURIComponent(raw_query[key]);
	}

	// set the parsed query back onto the event.
	event.query = query;
	event.queryStringParameters = query;

	return AWSServerlessExpress.proxy(server, event, context);
};

The trick was parsing the encoded query parts, annoyingly ALB did this automatically.

I set them back onto both queryStringParameters (ALB) and query (API Gateway) so it's compatible with both event types.

Do you think it would make sense to merge the decodeURIComponent stuff into the library?

It seems to be harmless if things are already decoded.

@emilionavarro
Copy link

Nice job man! I'm trying to do the same thing for ELB now. This helps a whole bunch!

@brett-vendia
Copy link
Contributor

Nice! This is natively supported in v4

fredericgermain added a commit to fredericgermain/serverless-express that referenced this issue Mar 25, 2021
fredericgermain added a commit to fredericgermain/serverless-express that referenced this issue Mar 25, 2021
github-actions bot pushed a commit that referenced this issue Jun 9, 2021
## [4.3.9](v4.3.8...v4.3.9) (2021-06-09)

### Bug Fixes

* auto-unescape query parameters on ALB ([#219](#219), [#241](#241)) ([#393](#393)) ([8cb4206](8cb4206))
OneDev0411 added a commit to OneDev0411/serverless-express that referenced this issue Mar 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants