How to Deploy Node Express API to Vercel Serverless Functions
So you’ve built an awesome API in your local machine with Express using Node.js and you are ready for clients and your parents to be proud of you?
Well, you need to push your API to the cloud, aka production.
Then you run into the plethora of options Azure, Google Cloud Platform, AWS, Heroku, etc…….
for deploy on AWS — checkout this medium How to deploy Node Express API to EC2 instance in AWS
But wait did you know you could also deploy your API to Vercel? Not only deploy but it will even listen when you push your changes to your main
Git Branch.
Pre-requisites:
- Vercel Free account — https://vercel.com/signup
- If you don’t have NODE & NPM installed you need to 😊 check out this article
- Have or create an API (we’ll create a simple one if you don’t have one already)
Overview of steps:
- Create local Node Express API
- Connect local API to a Github repo
- Connect your Vercel account with that Github repo
- Update your repo locally then watch changes deploy immediately
Steps breakdown
1. Create Express API with NODE
- Create a project directory, like
mkdir node-api-vercel
- Go into the new folder, with
cd node-api-vercel
- Initialize this folder as a Node project:
npm init -y
- Create files index.js, vercel.json, .gitignore files:
touch index.js vercel.json .gitignore
- Install Express.js:
npm i express
Use these boilerplates for your .gitignore, index.js, vercel.json files
2. Connect local API folder to a Github repo
a. Create a repository in Github, node-api-vercel
b. Connect Repo in Github with your API following instructions in Github
c. Push API folder node-api-vercel
to Github
3. Connect your Vercel account with that Github repo
a. Log into your Vercel account then click on New Project
Link should look like this https://node-express-api-vercel.vercel.app/
Enjoy…..🍻
More details on what has happened here, Vercel takes the exported app
and converts it to Serverless functions hosted in Vercel, for more info in regards visit https://vercel.com/guides/using-express-with-vercel