DEV Community

Cover image for Deploy Your Backend Securely and Effortlessly with Vercel
Muhdsodiq Bolarinwa
Muhdsodiq Bolarinwa

Posted on

2

Deploy Your Backend Securely and Effortlessly with Vercel

Why Deploy Your Backend on Vercel?

When you're working on a personal project that needs a backend (server-side code).

Vercel enforces HTTPS for your backend. This ensures safe communication between your front end and back end. This is critical. Browsers block mixed content (HTTP within HTTPS pages) to stop security holes. Deploying on Vercel lets you avoid this browser restriction. It also guarantees a secure user experience.
This makes it a budget-friendly solution for deploying backend code. This allows you to experiment and build without worrying about upfront costs.

Getting Started with Vercel Deployment

Here's a guide to deploying your backend on Vercel. This works even if you're using a MongoDB as your backend storage.

Project Setup

Create a new directory for your project.

 mkdir deployvercel
Enter fullscreen mode Exit fullscreen mode

Image description

Initialize a Node.js project using yarn init or npm init.

Install Express.js

yarn add express
Enter fullscreen mode Exit fullscreen mode

or

npm install express
Enter fullscreen mode Exit fullscreen mode

Image description

Create a file named index.js and define your backend logic within this file. This includes setting up Express routes. It means defining API endpoints and handling database interactions (if needed).

Run the Application Locally

Add a script to your package.json file to run your backend application locally:

JSON

scripts": {
    "dev": "nodemon index.js",
  }
Enter fullscreen mode Exit fullscreen mode

Image description

To start the backend server Run

npm run dev or yarn dev
Enter fullscreen mode Exit fullscreen mode

Image description

Configure Vercel Deployment (vercel.json)

Put a vercel.json file in your project's root directory. It tells Vercel how to handle your backend code. Here is the configuration:

{
    "version": 2,
    "builds": [
      {
        "src": "index.js",
        "use": "@vercel/node"
      }
    ],
    "routes": [
      {
        "src": "/(.*)",
        "dest": "index.js",
        "methods": ["GET", "POST", "PUT", "PATCH", "DELETE",  "OPTIONS"]
      }
    ]
  }
Enter fullscreen mode Exit fullscreen mode

This configuration tells Vercel to use its Node.js build process. It handles all requests to your backend using the index.js file.

Deploy to Vercel Using Git

Push your code to GitHub.
In your Vercel dashboard, add a new project and connect it to your Git repository.

Vercel will find your vercel.json file. It will deploy your backend when you push code changes to your Git repository.
With these steps, you'll have a secure backend. It will be scalable and globally distributed. It will be deployed on Vercel and ready to serve your frontend or API.
Image description
Image description

Github Url

https://github.com/Amity808/deployVercel-tutorial
Enter fullscreen mode Exit fullscreen mode

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay