How to Deploy Express.js Functions on Vercel: A Step-by-Step Guide

Hardik Desai avatar
Hardik Desai

23 September 2024

2 min read
How to Deploy Express.js Functions on Vercel: A Step-by-Step Guide

Description

Start with a brief introduction to the importance of deploying applications and APIs, and mention why Vercel is a suitable platform for this purpose.

Summary

Embark on a seamless journey from code to the cloud! In this guide, we'll unravel the magic of deploying Node.js/Express APIs seamlessly on Vercel, paired with the power of MongoDB Atlas. Elevate your projects to new heights as we navigate through the intricacies of setting up, testing locally, and finally launching your API into the cloud. Buckle up for a hands-on experience that transforms your development workflow and propels your Node.js applications into the world of effortless deployment. Let's turn your localhost endeavors into a cloud-powered success story!

Installation Guide

1. Using the Node.js Runtime with Serverless Functions

1
npm i @vercel/node

2. default dependency for express project

1
npm i dotenv express mongoose

Source Code

1
const express = require("express");
2
3
const app = express();
4
5
require("dotenv").config();
6
7
app.use(express.json());
8
9
// ** Packages
10
const connectDB = require('./connection/connectToMongo');
11
const RestaurantModel = require("./models/book");
12
13
connectDB()
14
15
// ** Constant
16
const PORT = 5000;
17
18
app.get('/', async (req, res) => {
19
res.status(200).json({
20
message: 'Hello World'
21
})
22
})
23
24
app.get("/api/hello", async (req, res) => {
25
const list = await RestaurantModel.find();
26
if (list) {
27
res.status(200).json({ restaurant: list });
28
} else {
29
res.status(400).json({ message: "Failed To Retrieve Blogs" });
30
}
31
});
32
33
// ** Server
34
app.listen(PORT, () => {
35
console.log(`Server is running on port http://localhost:${PORT}`)
36
})

Join the newsletter

Subscribe for weekly updates. No spams ever!

Copyright © 2024 | Hardik Desai | All Rights Reserved