Create Proxy Server

Creating and deploying a tiny proxy server on Vercel in 10 minutes · mmazzarolo.com

项目设置

1
2
3
4
5
mkdir my-proxy && cd my-proxy
npm init
npm install -D vercel
mkdir api && touch api/index.js
touch vercel.json

my-proxy/package.json

1
2
3
4
5
6
7
{
  "name": "my-proxy",
  "version": "1.0.0",
  "scripts": {
+    "start": "vercel dev"
  },
}

Vercel serverless functions use a file-system-based convention. So the api/index.js file you just created will automatically handle all requests of the /api endpoint:

my-proxy/api/index.js

1
2
3
4
5
6
7
8
9
// In Vercel, any file inside the "api" directory is exposed on an "/api" endpoint.
// For an API route to work, you need to export a function as default (a.k.a request handler),
// which then receives the following parameters:
// - req: The request object.
// - res: The response object.
// See https://vercel.com/docs/serverless-functions/supported-languages#node.js for details.
export default async function handler(req, res) {
  res.status(200).send(`Hello world!`)
}

my-proxy/vercel.json

1
2
3
{
  "rewrites": [{ "source": "/api/(.*)", "destination": "/api" }]
}

代理逻辑设置

1
npm i http-proxy-middleware

修改 my-proxy/api/index.js

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Create a proxy to redirect requests of the "/api/*" path to "https://example.org".
//
// Examples:
// GET /api/hello → GET https://example.org/hello
// POST /api/test?color=red → POST https://example.org/test?color=red
//
// Additionally, the proxy will:
// - Add an "x-added" header
// - Remove the "x-removed" header
// From the proxied response.
//
// You can/should update the proxy to suit your needs.
// See https://github.com/chimurai/http-proxy-middleware for more details.
const { createProxyMiddleware } = require('http-proxy-middleware')
const apiProxy = createProxyMiddleware({
  target: 'https://example.org',
  changeOrigin: true,
  pathRewrite: {
    '^/api': '', // strip "/api" from the URL
  },
  onProxyRes(proxyRes) {
    proxyRes.headers['x-added'] = 'foobar' // add new header to response
    delete proxyRes.headers['x-removed'] // remove header from response
  },
})
// Expose the proxy on the "/api/*" endpoint.
export default function (req, res) {
  return apiProxy(req, res)
}

Layout of comment panels