DEV Community

Mohamed Hajith.M
Mohamed Hajith.M

Posted on

Deploy an Express.js API on Vercel 🚀

Deploying Express.js on Vercel: Essential Steps

This guide outlines the minimum steps required to deploy a simple Express.js application to Vercel.


Step 1: Create index.js

Start by creating the index.js file as the main entry point of your Express application:

// index.js (Main entry point for Vercel)

const express = require('express');

const app = express();
const port = 3000;

app.use(express.json()); // Middleware to parse JSON requests

const JWT_SECRET = 'your_secret_key';

app.get('/', (req, res) => {
    res.send('Hello, World - Todo Api!');
});

// Other routes...
// Add additional routes as needed
.....

// Export the Express app
module.exports = (req, res) => {
    app(req, res);
};
Enter fullscreen mode Exit fullscreen mode

This file serves as the main server code for your application. Vercel uses this exported function to handle incoming requests.


Step 2: Create or Update vercel.json

If a vercel.json file does not already exist in your project, create one in the root directory. If it exists, update it with the following configuration:

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

Explanation:

  • builds: Defines how Vercel builds the project. The @vercel/node builder handles Node.js applications.
  • routes: Routes all requests to index.js.

Deployment

  1. Install the Vercel CLI (if not already installed):
   npm install -g vercel
Enter fullscreen mode Exit fullscreen mode
  1. Deploy your application:
   vercel
Enter fullscreen mode Exit fullscreen mode

Follow the prompts to complete the deployment. Vercel will provide you with a live URL for your application.


Your Express.js application is now live on Vercel! 🎉

Top comments (0)