Skip to content

Instantly share code, notes, and snippets.

@elcodabra
Created June 15, 2020 10:45
Show Gist options
  • Save elcodabra/43ef0157464d92bfe1e6eb7b40c94771 to your computer and use it in GitHub Desktop.
Save elcodabra/43ef0157464d92bfe1e6eb7b40c94771 to your computer and use it in GitHub Desktop.
Next JS 9: Proxy middleware with api and next-connect
// [...slug].js
import nc from 'next-connect'
const dev = process.env.NODE_ENV !== 'production'
const handler = nc()
if (dev) {
const { createProxyMiddleware } = require('http-proxy-middleware')
handler.use(createProxyMiddleware({
target: process.env.BACKEND_API,
pathRewrite: {
[`^/${process.env.API_PATH}`]: '',
},
changeOrigin: true,
}))
}
export default handler
// next.config
module.exports = {
optionalCatchAll: true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment