DEV Community

checode
checode

Posted on • Edited on

1

How to deploy a Backend project on Vercel | Node.js - Express.js šŸš€

Deploying a backend project with NodeJS and Express on Vercel is pretty straightforward. You just need to keep a few things in mind! I'll walk you through it step by step in this article, and I'll also provide the sample code so you can copy it and try it out in your project.

I’m also sharing the step-by-step process in a video.

Don’t forget to subscribe to my channel ā¤ļø.

.

Preparing the project for deployment on Vercel

We'll start by creating a simple project to deploy. In your code editor, create a folder named app. Inside this folder, run the following command in the console:

npm init -y

This will create a package.json file with basic and necessary project information.

Now, inside our app folder, create another file that we’ll call index.js. It’s crucial that it's named index.js and not something like app.js or server.js because Vercel will look for a file named index.js when deploying the code.

So the structure should look something like this:

project structure

Alright, let’s start writing some code in our index.js. Here's a basic example you can use as a reference.

const express = require("express");
const app = express();

const port = process.env.PORT || 3000;

app.get("/", (req, res) => {
  const htmlResponse = `
    <html>
      <head>
        <title>NodeJs and Express on Vercel</title>
      </head>
      <body>
        <h1>I am a backend project on Vercel</h1>
      </body>
    </html>
  `;
  res.send(htmlResponse);
});

app.listen(port, () => {
  console.log(`port running at http://localhost:${port}`);
});

Enter fullscreen mode Exit fullscreen mode

At this point, you likely have your own project or are thinking of deploying something else, but let me give you a quick explanation of the code above—it might be useful.

The first thing it's doing is requiring Express and then invoking it in the app constant.

const express = require("express");
const app = express();
Enter fullscreen mode Exit fullscreen mode

Since we're using Express, we need to install it for it to work šŸ˜…
Run this command in the console, always inside the app folder:

npm i express

The second thing (and very important) is the port configuration in the port constant.

const port = process.env.PORT || 3000;

Here, we’re telling it that our project will run on a specific domain, or in our case, on a URL that Vercel will generate for us. We indicate this with (process.env.PORT). Then we tell it that if no domain/URL is configured, it will run on our local server at port 3000 with this: ( || 3000). But no worries! Vercel will configure a URL for us!

Now for the third step, but the important part is already done in step 2. The rest is more about adjusting it to whatever happens in your project. In my case, I’ve just set up some basic HTML code to display in the browser when the server runs.

app.get("/", (req, res) => {
  const htmlResponse = `
    <html>
      <head>
        <title>NodeJs and Express on Vercel</title>
      </head>
      <body>
        <h1>I am a backend project on Vercel</h1>
      </body>
    </html>
  `;
  res.send(htmlResponse);
});
Enter fullscreen mode Exit fullscreen mode

Finally, we ask the app constant (where Express is running) to listen to the port we configured earlier and give us some feedback in the console.

app.listen(port, () => {
  console.log(`port running at http://localhost:${port}`);
});
Enter fullscreen mode Exit fullscreen mode

Now we're ready to test the project on our local server (it will run on port 3000) to check if everything is working. Run this command in the console:

node index.js

Did it work? Well, if everything went well, meaning there were no errors and your project ran successfully on your local server at port 3000, then we're ready to upload it to Vercel.

Preparing project configurations for deployment on Vercel

At this stage, the first thing we need to do is create a JSON file called vercel.json (again, always inside the app folder). Inside this file, we'll add the basic configurations Vercel needs, like this:

{
  "builds": [
    {
      "src": "./index.js",
      "use": "@vercel/node"
    }
  ],
  "routes": [
    {
      "src": "/(.*)",
      "dest": "/"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

I don’t want to go too deep into this, but these are the necessary configurations for the "builds" and "routes" that we need to make the deployment work.

A brief explanation:

Builds: These define how the application is built. In this case, we use our index.js file within the @vercel/node runtime environment.

Routes: These define how the application’s routes are handled. All URLs represented by /(.*) will redirect to the site’s root (/).

At this point, your project structure should look something like this (or so I hope).

project structure

Deploying the project on Vercel

Now for the final part: it’s finally time to upload this to Vercel. First, you need to create an account if you don’t already have one. Creating an account on Vercel is completely free, and they won’t ask for a credit card or anything like that. You can create an account here: https://vercel.com.

There are two ways to deploy: one is by linking a repository, and the other is by using the Vercel CLI and doing everything through the console. I prefer using the console because it’s very simple, and there’s no need to upload everything to GitHub. Here’s how to do it.

If you’ve never installed Vercel before, now is the time!
Run the following command in your console to install Vercel globally:

npm install -g vercel

Now we can use the CLI and upload the project via the console.
Simply run the vercel command.

vercel

Once you run that command, a few simple questions will appear in your console that you need to answer.

Here they are with explanations:

vercel questions

Set up and deploy:
Here, type ā€œyā€ for yes.

Which scope do you want to deploy to?
Here, it asks which scope you want to deploy to. I see pab-mchn because that’s my account name, and I’m logged in. If you’re not logged in yet, it will prompt you to log in. It’s a simple process managed through a browser confirmation.

Link to existing project?
Here, it asks if you want to link this to an existing project on Vercel. Type ā€œnā€ for no, unless you want to link it to a project you’ve already created.

What’s your project’s name?
Here, it just asks for the name you want to give your project. Create one or use the name of your project if you answered ā€œyesā€ to the previous question.

In which directory is your code located?
Here, it asks where your project is located and suggests ./ as the answer. This is correct—our index.js file is in the project’s root inside the app folder, so the location is right. Simply press "Enter."

And that’s it! Vercel will generate the links to deploy the project, and you’ll be able to view it at the URLs displayed in the console. Open them in your browser.

If you want to push it to production, you can run this command (it’s suggested in the console too):

vercel --prod

That’s it!

I hope you get your awesome project up on Vercel, and have a good time doing it.

Best regards,
Pablo.

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil — patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Top comments (1)

Collapse
 
oscarrickovic profile image
Oscar Rickovic •

Like it šŸ˜Ž, it’s brief resume but more than enough

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

šŸ‘‹ Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay