Open In App

How to Deploy Node Backend on Vercel ?

Last Updated : 08 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Screenshot-2024-03-05-114336

Step 5: Write the following Code for index.js and json for vercel.json

Javascript




// index.js
import http from 'http';
 
// Create a server object
const server = http.createServer((req, res) => {
    // Set the response header
    res.writeHead(200, {'Content-Type': 'text/plain'});
    // Write some text to the response
    res.end('Welcome to my simple Node.js app!');
});
 
// Define the port to listen on
const port = 3000;
 
// Start the server
server.listen(port, () => {
    console.log(`Server is running on http://localhost:${port}`);
});


Javascript




// vercel.json
 
{
    "version":2,
    "builds":[
        {
            "src":"*.js",
            "use":"@vercel/node"
        }
    ],
    "routes":[
        {
            "src":"/(.*)",
            "dest":"/"
        }
    ]
}


Step 6: Deploy Express App to Vercel by running the following command.

vercel
Screenshot-2024-03-05-120224

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.

Screenshot-2024-03-05-120859

Output for nodejs app



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads