Skip to content

Commit

Permalink
docs(nextjs): improve nextjs example (#945)
Browse files Browse the repository at this point in the history
  • Loading branch information
chimurai committed Oct 16, 2023
1 parent d286ac8 commit a703969
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions recipes/servers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<NextApiRequest, NextApiResponse>({
// singleton
export const proxyMiddleware = createProxyMiddleware<NextApiRequest, NextApiResponse>({
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) => {
Expand Down

0 comments on commit a703969

Please sign in to comment.