A small reverse proxy API middleware for Vercel Serverless Functions lands. Often used in combination with Vercel frontend apps.
- Add the destination API URLs and/or Tokens as Environment Variables in the Vercel dashboard during build. API_TOKEN, API_URL etc.
- Create one .js file for every proxy API endpoint you would like to add. i.e. for /api/path create an /api/path.js file
- Add any additional headers in the proxy.js file
- Add the following to path.js
/**
* Module dependencies
*/
const express = require("express");
const proxy = require("./proxy");
/**
* Expose the app
*/
const app = (module.exports = express());
/**
* Mount the proxy middleware
*/
app.use("/api/path", proxy(process.env.API_URL));
- Make the request to
https://your-vercel-domain/api/path
to reach the https://destination-API-endpoint/
Inspired by simple-http-proxy.