This project demonstrates how to set up two different API routes on Vercel: one using Express and another using module exports.
- Express API: A route that handles requests using an Express server.
- Module Exports API: A route that uses module exports for handling requests without a server.
src/
├── express.js # Express API
├── module.js # Module exports API
├── index.js # Main entry point for routing
package.json # Dependencies and scripts
vercel.json # Vercel configuration for routing
readme.md # Project description
-
Module Exports Route:
/api/module- Description: This route is handled by the
module.jsfile. It uses a lightweight, serverless function to respond to requests. - Response: Returns a simple JSON message like
{"message": "Hello from Module Export on Vercel!"}.
- Description: This route is handled by the
-
Express Route:
/api/express- Description: This route is handled by the
express.jsfile. It uses an Express server to manage requests and responses. - Response: Returns a JSON message like
{"message": "Hello from Express on Vercel!"}.
- Description: This route is handled by the
- express : https://vercel-test-xi-virid.vercel.app/api/express
- module : https://vercel-test-xi-virid.vercel.app/api/module
The vercel.json file defines the routing for the two APIs:
{
"version": 2,
"builds": [
{
"src": "src/*.js",
"use": "@vercel/node"
}
],
"routes": [
{
"src": "/api/module",
"dest": "/src/module.js"
},
{
"src": "/api/express",
"dest": "/src/express.js"
},
{
"src": "/",
"dest": "/src/main.js"
}
]
}-
API Routes section clearly defines the two routes (
/api/moduleand/api/express). -
Each route is described in detail, including the file that handles the route and the type of response users can expect.
-
and
/for main route that is default url route
This simple structure should make it easy to understand what each route does.