In JavaScript, “proxy” can mean three things. A CORS proxy is a server that fetches an API for your web page. The Next.js proxy is a file that runs before a request reaches your route. The Proxy object wraps another object in your code. None of them changes the IP address your visitors or scrapers use, which is the job of a network proxy such as residential proxies.
| What it is | Where it runs | Use it to | |
|---|---|---|---|
| CORS proxy | A small server | Your backend | Read an API that browsers block |
| Next.js proxy | A proxy.ts file | Your Next.js server | Redirect, rewrite or check requests |
| JavaScript Proxy | A built-in object | Inside your code | Control access to an object |
| Network proxy | A server with its own IP | Outside your app | Send requests from another address |
CORS proxy
A browser does not let a page read a response from another origin unless that server allows it. When the permission is missing, Chrome prints this:
Access to fetch at 'http://127.0.0.1:4701/price' from origin
'http://127.0.0.1:4703' has been blocked by CORS policy: No
'Access-Control-Allow-Origin' header is present on the requested resource.
Only browsers enforce this rule. A server can request the same address freely, so a CORS proxy makes the request for you and adds the missing header. This one runs on Node.js without any packages:
import http from 'node:http';
const TARGET = 'https://api.example.com';
const ALLOWED = 'https://www.example.com';
http.createServer(async (req, res) => {
const headers = {
'Access-Control-Allow-Origin': ALLOWED,
'Access-Control-Allow-Methods': 'GET, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
Vary: 'Origin',
};
if (req.method === 'OPTIONS') { res.writeHead(204, headers); return res.end(); }
if (req.method !== 'GET') { res.writeHead(405, headers); return res.end(); }
const url = new URL(req.url, TARGET);
if (url.origin !== TARGET) { res.writeHead(400, headers); return res.end(); }
const upstream = await fetch(url);
res.writeHead(upstream.status, {
...headers,
'Content-Type': upstream.headers.get('content-type') ?? 'text/plain',
});
res.end(await upstream.text());
}).listen(3001, '127.0.0.1');
We ran this code with local addresses in Chrome. The direct request failed with the error above, and the same request through the proxy returned the data. Before using it for real, add error handling, a timeout and a rate limit.
The fixed TARGET matters. A proxy that fetches any address it is given can be used by strangers to reach other sites, or private systems on your own network. The same goes for public CORS proxies: their operator can read everything you send, so never pass keys or customer data through one.
If you control the API, you need no proxy at all. Send the Access-Control-Allow-Origin header from the API itself.
Next.js proxy
Next.js uses the word for two separate features.
The proxy.ts file runs on the server before a route is rendered. It was called middleware until Next.js 16, which deprecated the old name. Put it next to your app or pages folder:
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function proxy(request: NextRequest) {
return NextResponse.redirect(new URL('/home', request.url));
}
export const config = { matcher: '/about/:path*' };
Set a matcher. Without one, the file runs on every request, including images and scripts. To move an older project over, Next.js offers a command:
npx @next/codemod@canary middleware-to-proxy .
Rewrites forward a path to another server. The browser only talks to your own domain, so no CORS error appears:
// next.config.js
module.exports = {
async rewrites() {
return [
{ source: '/api/shop/:path*', destination: 'https://api.example.com/:path*' },
];
},
};
For most Next.js projects this replaces a separate CORS proxy. If the API needs a secret key, use a route handler, so the key stays on the server. Both features are forms of a reverse proxy, and our post on forward and reverse proxies explains that difference.
JavaScript Proxy object
Proxy is part of the language. It wraps an object and runs your function when a property is read, written or deleted:
const prices = new Proxy({ us: 10 }, {
get: (target, key) => (key in target ? target[key] : 'not set'),
});
console.log(prices.us); // 10
console.log(prices.de); // not set
Common uses are default values, validation, logging and change tracking. Vue 3, for example, builds its reactive objects on it. Each access goes through your function, so avoid it in code that runs millions of times.
When you need a network proxy instead
A CORS proxy sends every request from your server’s IP address. If the target site limits that address or shows different content by country, changing headers will not help. Route the server’s requests through a proxy with the location you need. Our guide to residential proxies in Node.js has the code.