How to Deploy Node Backend on Vercel ?
Last Updated :
08 Mar, 2024
Deploying a Node app on Vercel is really easy. You just need to run a few commands in your computer’s command prompt. Vercel works well with Git, so whenever you make changes to your code and push them to your Git repository, Vercel automatically updates your deployed app. Vercel also makes sure your app runs fast and can handle lots of users without crashing. Plus, it gives you helpful tools to see how many people are using your app and if there are any problems you need to fix. Overall, Vercel makes it simple to get your Node.js app up and running on the internet for everyone to use.
Here we are going to make simple text based application and deploy it with vercel CLI.
Approach to deploy Nodejs App to vercel
- Install the Vercel CLI and log in with your Vercel account.
- Create an node.js project and export the node app.
- Configure the vercel.json to redirect the requests to our node app.
- Deploy the application to Vercel with the Vercel CLI.
Steps to Deploy Node Application to Vercel
Step 1: Create a folder for the project and deployment
mkdir deploy_nodejs_vercel
cd deploy_nodejs_vercel
Step 2: Initialized the node application
npm init
Step 3: Add `http` dependency to connect app to http and vercel for deployment.
npm i http
npm i -g vercel@latest
Step 4: Make following Js files in the directory or directory should look like this.
Folder Structure:
Step 5: Write the following Code for index.js and json for vercel.json
Javascript
import http from 'http' ;
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type' : 'text/plain' });
res.end( 'Welcome to my simple Node.js app!' );
});
const port = 3000;
server.listen(port, () => {
console.log(`Server is running on http:
});
|
Javascript
{
"version" :2,
"builds" :[
{
"src" : "*.js" ,
"use" : "@vercel/node"
}
],
"routes" :[
{
"src" : "/(.*)" ,
"dest" : "/"
}
]
}
|
Step 6: Deploy Express App to Vercel by running the following command.
vercel
project setup for vercel
Incase you need to redeploy after making changes use the vercel command with prod flag.
vercel --prod
Output: Now run the deployed link on the browser.
Output for nodejs app
Share your thoughts in the comments
Please Login to comment...