Skip to content

Instantly share code, notes, and snippets.

@KrisKnez
Created June 2, 2024 11:10
Show Gist options
  • Save KrisKnez/28978bbf0e7c3908e91673b3e80898fb to your computer and use it in GitHub Desktop.
Save KrisKnez/28978bbf0e7c3908e91673b3e80898fb to your computer and use it in GitHub Desktop.
Deploy NEST.JS to Vercel Serverless Functions
// vercel.json
{
"version": 2,
"builds": [
{
"src": "package.json",
"use": "@vercel/static-build",
"config": {
"distDir": "public"
}
},
{
"src": "vercel-func.js",
"use": "@vercel/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "vercel-func.js"
}
]
}
// vercel-func.js
import { HttpAdapterHost } from '@nestjs/core';
import { getBootstrapApp } from './dist/main';
// Keep the app instance in memory for subsequent requests
let app;
export default async function handler(req, res) {
// This will be called on cold start
if (!app) app = await getBootstrapApp();
const adapterHost = app.get(HttpAdapterHost);
const httpAdapter = adapterHost.httpAdapter;
const instance = httpAdapter.getInstance();
instance(req, res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment