API Proxy Setup in Next.js Dev Environment
I’m looking for a way to set up API proxying in my Next.js project. Previously, when I worked with create-react-app, I had a proxy file that managed the API calls automatically.
Here’s the setup I used:
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function(app) {
app.use('/api',
createProxyMiddleware({
target: 'http://localhost:8000',
changeOrigin: true,
})
);
};
Unfortunately, this setup doesn’t work in Next.js, as I keep encountering various errors. I’ve read that creating a custom server might be a solution, but I prefer to use the default development server. Can I implement similar proxy capabilities while running just the next dev command without resorting to additional server configurations?