AWS Builder Center

Why My Express API Crashed on AWS Lambda (And the 1 Line Fix)

Converting a standard Node.js Express app to run on AWS Lambda seems simple—until your API routes start throwing random 502 Bad Gateway errors. Here is why standard server routing breaks on serverless and how to fix it fast.

When I first learned how to deploy Node.js applications, Express.js was the go-to web framework. You set up your routes like  app.get('/users') , call  app.listen(3000) , and your server runs continuously in the background listening for incoming HTTP requests.
For a recent class project, I wanted to take my existing Express API and host it on AWS Lambda to take advantage of serverless scaling.
I uploaded my code, set up API Gateway in front of the function, and tried hitting the URL.
Result? A blank page with a cryptic error message:
JSON
{ "message": "Internal server error" }
In CloudWatch, I kept seeing  HTTP 502 Bad Gateway  errors. My Express app wasn't crashing because of a code bug—it was crashing because of how AWS Lambda handles HTTP events under the hood.
Here is what went wrong and how to fix it without rewriting your entire backend.

The Problem: Lambda Doesn't Listen on Ports

Standard web servers run forever, waiting for TCP connections on a port (like  3000  or  8080 ).
AWS Lambda doesn't work like that. It is an event-driven function. When a user sends a web request through AWS API Gateway, AWS translates that request into a JSON object (an "event") and passes it directly into your Lambda handler function.
Plaintext
[ Browser Request ] ---> [ API Gateway (JSON Event) ] ---> [ Lambda Handler ]
|
(Express isn't listening!)
Because an un-modified Express app expects raw HTTP socket connections on  app.listen() , it completely ignores the JSON event object from API Gateway.

The Fix: Wrap Express with  serverless-express 

Instead of rewriting all your Express routes to native AWS Lambda syntax, you can use a lightweight wrapper library like  @codegenie/serverless-express  (or  aws-serverless-express ).
This library sits between API Gateway and your Express app. It translates the incoming API Gateway JSON event into a standard Node.js HTTP request object that Express can understand, and then formats Express's response back into JSON for API Gateway.

Step 1: Install the wrapper

In your project folder, run:
Bash
npm install @codegenie/serverless-express

Step 2: Update your  index.js  / entry point

Instead of calling  app.listen() , export the wrapped serverless handler:
JavaScript
const express = require('express');
const serverlessExpress = require('@codegenie/serverless-express');

const app = express();

// Your normal Express routes
app.get('/api/hello', (req, res) => {
res.json({ message: "Hello from AWS Lambda!" });
});

app.get('/api/students', (req, res) => {
res.json({ students: ["Alex", "Jordan", "Taylor"] });
});

// IMPORTANT: Replace app.listen() with serverlessExpress
exports.handler = serverlessExpress({ app });

2 Small Details to Watch Out For

  1. Enable Proxy Integration in API Gateway: When configuring your API Gateway routes, make sure you use the  ANY /{proxy+}  greedy path with HTTP Proxy Integration turned on. This passes all sub-paths (like  /api/hello  or  /api/students ) directly to your single Lambda function so Express can do the routing.
  1. Watch your binary payloads: If your Express app returns images or PDF downloads, you need to specify binary mime types inside the  serverlessExpress  config, otherwise API Gateway will corrupt the file output trying to parse it as UTF-8 text.
Once I wrapped the app and updated the gateway route, every single Express route started working instantly!
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article