Created
June 2, 2024 11:10
-
-
Save KrisKnez/28978bbf0e7c3908e91673b3e80898fb to your computer and use it in GitHub Desktop.
Deploy NEST.JS to Vercel Serverless Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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