Created
June 15, 2020 10:45
-
-
Save elcodabra/43ef0157464d92bfe1e6eb7b40c94771 to your computer and use it in GitHub Desktop.
Next JS 9: Proxy middleware with api and next-connect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// [...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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// next.config | |
module.exports = { | |
optionalCatchAll: true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment