From a7039693e6094546ead45f462ce1e79ec8d0d616 Mon Sep 17 00:00:00 2001 From: chimurai <655241+chimurai@users.noreply.github.com> Date: Mon, 16 Oct 2023 23:15:09 +0200 Subject: [PATCH] docs(nextjs): improve nextjs example (#945) --- recipes/servers.md | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/recipes/servers.md b/recipes/servers.md index 15a527e0..45cb656f 100644 --- a/recipes/servers.md +++ b/recipes/servers.md @@ -65,20 +65,26 @@ https://github.com/vercel/next.js [![GitHub stars](https://img.shields.io/github/stars/vercel/next.js.svg?style=social&label=Star)](https://github.com/vercel/next.js) ![next.js downloads](https://img.shields.io/npm/dm/next) -Next project: `/pages/api/users.ts` - ```typescript -// Next.js API route support: https://nextjs.org/docs/api-routes/introduction -import type { NextApiRequest, NextApiResponse } from 'next'; +// Next project: `/pages/api/users.proxy.ts` import { createProxyMiddleware } from 'http-proxy-middleware'; -const proxyMiddleware = createProxyMiddleware({ +// singleton +export const proxyMiddleware = createProxyMiddleware({ target: 'http://jsonplaceholder.typicode.com', changeOrigin: true, pathRewrite: { '^/api/users': '/users', }, }); +``` + +```typescript +// Next project: `/pages/api/users.ts` + +// Next.js API route support: https://nextjs.org/docs/api-routes/introduction +import type { NextApiRequest, NextApiResponse } from 'next'; +import { proxyMiddleware } from './users.proxy'; export default function handler(req: NextApiRequest, res: NextApiResponse) { proxyMiddleware(req, res, (result: unknown) => {