Skip to content

Commit

Permalink
docs(nextjs): improve nextjs example
Browse files Browse the repository at this point in the history
  • Loading branch information
chimurai committed Oct 16, 2023
1 parent d286ac8 commit 1c9f680
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 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 Expand Up @@ -170,7 +176,7 @@ app.use(
createProxyMiddleware({
target: 'http://www.example.org',
changeOrigin: true,
}),
})
);

app.listen(3000);
Expand Down Expand Up @@ -345,7 +351,7 @@ gulp.task('webserver', function () {
directoryListing: true,
open: true,
middleware: [apiProxy],
}),
})
);
});

Expand Down

0 comments on commit 1c9f680

Please sign in to comment.