Skip to content

Commit

Permalink
docs(examples): next proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
chimurai committed Apr 27, 2024
1 parent 68952c3 commit 83e886b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
12 changes: 12 additions & 0 deletions examples/next-app/pages/api/_proxy.ts
@@ -0,0 +1,12 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { createProxyMiddleware } from '../../../../dist';

// singleton
export const proxyMiddleware = createProxyMiddleware<NextApiRequest, NextApiResponse>({
target: 'http://jsonplaceholder.typicode.com',
changeOrigin: true,
pathRewrite: {
'^/api/users': '/users',
},
logger: console,
});
19 changes: 19 additions & 0 deletions examples/next-app/pages/api/users.ts
@@ -0,0 +1,19 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { proxyMiddleware } from './_proxy';

export default function handler(req: NextApiRequest, res: NextApiResponse) {
proxyMiddleware(req, res, (result: unknown) => {
if (result instanceof Error) {
throw result;
}
});
}

export const config = {
api: {
externalResolver: true,
// Uncomment to fix stalled POST requests
// https://github.com/chimurai/http-proxy-middleware/issues/795#issuecomment-1314464432
bodyParser: false,
},
};

0 comments on commit 83e886b

Please sign in to comment.