Prompt
How do I deploy a Node.js API to serverless?
Latest observation
Oct 3, 2026
OpenAI APIWeb search: off
Deploying a Node.js API to serverless usually means packaging your API so it runs as functions behind an API gateway, instead of as a constantly running server.
Common options
- AWS Lambda + API Gateway
- Google Cloud Functions / Cloud Run
- Azure Functions
- Vercel / Netlify Functions
- Cloudflare Workers (best for edge-compatible code)
Typical approach
-
Make your API serverless-friendly
- Avoid
app.listen(...) - Export the app or handler instead
- Keep startup code lightweight
- Avoid
-
Choose a framework or adapter
- Express → use an adapter like
serverless-http - Fastify → use
@fastify/aws-lambda - Native handlers work too
- Express → use an adapter like
-
Configure routing
- Map HTTP routes to function endpoints via your platform config
-
Deploy
- Use platform CLI / IaC:
- AWS SAM
- Serverless Framework
- Terraform
- Vercel CLI
gcloud,az, etc.
- Use platform CLI / IaC:
-
Test
- Test locally with the provider’s emulator or local dev tool
Example: Express on AWS Lambda
1) Install
npm install express serverless-http
2) Create your app
// app.js
const express = require("express");
const app = express();
app.use(express.json());
app.get("/hello", (req, res) => {
res.json({ message: "Hello from serverless!" });
});
module.exports = app;
3) Create Lambda handler
// handler.js
const serverless = require("serverless-http");
const app = require("./app");
module.exports.handler = serverless(app);
4) Deploy with Serverless Framework
# serverless.yml
service: my-api
provider:
name: aws
runtime: nodejs20.x
functions:
api:
handler: handler.handler
events:
- httpApi: "*"
5) Deploy
npx serverless deploy
Best practices
- Keep functions small and stateless
- Use a managed DB or external cache
- Reuse DB connections across invocations
- Minimize cold starts
- Bundle dependencies and remove unused code
- Put secrets in environment variables or secret managers
Important caveats
- Serverless isn’t ideal for:
- long-running jobs
- persistent websockets
- heavy CPU workloads
- Cold starts can impact latency
- Local debugging differs from production behavior
If you want, I can give you a step-by-step deployment guide for a specific platform like AWS Lambda, Vercel, or Google Cloud.