If you’ve built a backend with Node.js and MongoDB and want to deploy your Node.js app to Vercel, the good news is, it’s easier than you think! Vercel isn’t just for frontend frameworks; it supports backend APIs too, using serverless functions.
In this post, I’ll walk you through deploying a Node.js + MongoDB API project to Vercel, with all the required files and steps.
Before we get started, don’t forget to subscribe to my newsletter!
Get the latest tips, tools, and resources to level up your web development skills delivered straight to your inbox. Subscribe here!
Now let’s jump right into it!🚀
What We’ll Do to Deploy Node.js App to Vercel
- Install necessary packages
- Create a basic Node.js API using Express and MongoDB
- Push the project to GitHub
- Deploy to Vercel
- Test the live API
Step 1: Install Packages for Your Node.js App
First, create a new folder and initialize your Node.js project. Run the following commands inside your terminal:
mkdir my-api-app
This creates a new folder named my-api-app
. This is where your project files will live.
cd my-api-app
This command will move you into the my-api-app
folder so you can work inside it.
npm init -y
Initializes a new Node.js project by creating a package.json
file. The -y
flag skips the setup questions and uses default values.
If you want to change any of the default values, then run npm init
command instead of npm init -y
.
Now, install the required dependencies:
npm install express mongodb mongoose dotenv
This command installs the following packages:
- express: A minimal and flexible Node.js web framework to build APIs.
- mongodb: Official MongoDB driver to connect directly with your MongoDB database.
- mongoose: An ODM (Object Data Modeling) library for MongoDB, which simplifies working with the database in your Node.js app.
-
dotenv: Lets you load environment variables from a
.env
file, so sensitive data like database URLs aren’t hardcoded.
Step 2: Create Project Files for Node.js MongoDB API
Let’s set up the folders and files needed to build and deploy the API.
Set Up MongoDB Connection
Create a folder named “DB” inside the root directory, and inside that, create a file named “db.js” and add the following code to that file.
const mongoose = require("mongoose");
require("dotenv").config();
const connectToMongoDB = () => {
try {
mongoose.connect(process.env.MONGODB_URI).then(() => {
console.log("MongoDB is connected!");
});
} catch (error) {
console.log(error.message);
}
};
module.exports = connectToMongoDB;
This file connects to MongoDB using Mongoose and reads the URI from .env
.
Create Express API Entry Point
Create a folder named “api” inside the root directory, and inside that, create a file named “index.js” and add the following code to that file.
const express = require("express");
const connectToMongoDB = require("../DB/db.js");
const app = express();
app.use(express.json());
connectToMongoDB();
app.get("/api/hello", (req, res) => {
res.status(200).json({ message: "API is working!" });
});
module.exports = (req, res) => {
app(req, res);
};
This sets up an Express app and exports it as a Vercel serverless function.
This makes sure your API is up and connected to the database.
Create .env
Create a file named “.env” inside the root directory and add your MongoDB URI like this:
MONGODB_URI=<Your_MongoDB_Connection_String>/<YOUR_DATABASE_NAME>
Replace <Your_MongoDB_Connection_String>
with your connection string and <YOUR_DATABASE_NAME>
with your database name.
Add .gitignore
Create a file named “.gitignore” inside the root directory and add the following to that file:
.env
node_modules/
This prevents pushing sensitive data and unnecessary files to GitHub.
Create vercel.json to Deploy Node.js App to Vercel
Create a file named “vercel.json” inside the root directory and add the following code to that file:
{
"version": 2,
"builds": [
{
"src": "/api/index.js",
"use": "@vercel/node",
"config": { "includeFiles": ["dist/**"] }
}
],
"routes": [
{
"src": "/api/(.*)",
"dest": "/api/index.js"
}
]
}
This config tells Vercel how to deploy your API.
Final Project Structure to Deploy Node.js App to Vercel
Your folder should now look like this:
Step 3: Push Your Node.js App Code to GitHub
First, create a new repo on GitHub. Now, push your code, run the following commands one by one:
git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/your-username/my-api-app.git
git branch -M main
git push -u origin main
Your project is now on GitHub.
Step 4: Deploy Node.js App to Vercel
- Go to vercel.com/import
- Search for your repo name and then select your GitHub repo
- Change the project name if you want to
- Under Environment Variables, add:
- Key: MONGODB_URI
- Value: your MongoDB URI
- Click Deploy
That’s it! Vercel will install your packages, build the function, and deploy it automatically.
5. Test Your Deployed Node.js App API
Once deployed:
- Open your Vercel dashboard
- Click on your project
- Copy the domain
- Go to: “your-vercel-domain/api/hello”
You should see:
{ "message": "API is working!" }
🎉 That’s It!
You’ve successfully deployed your Node.js app to Vercel and tested your live API. You can share the link, connect it to your frontend, or build more endpoints.
That’s all for today!
For paid collaboration connect with me at : connect@shefali.dev
If you enjoy my work and want to support what I do:
👉 Become a Patreon supporter
👉 Or buy me a coffeeEvery small gesture keeps me going! 💛