[BR1]Sky
06/20/2022, 1:32 AMjavascript
const nextConfig = {
reactStrictMode: true,
async rewrites() {
return [
{
source: '/api/:path*',
destination: `${process.env["API_BASE_URL"]}/:path*`,
},
]
},
env: {
BASE_URL: process.env["BASE_URL"],
API_BASE_URL: process.env["API_BASE_URL"]
}
}
module.exports = nextConfig
When I make a post request from the client side like:
javascript
export default function Home() {
...
const req = await fetch(`/api/${endpoint}`, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(body)
});
The above rewrite, rewrites /api/ to my backend url, cookies are included and I can see that in the backend like this:
INFO: 127.0.0.1:52558 - "POST /auth/signin HTTP/1.1" 200 OK
USER: c4a258b4-d5a8-45cc-9fcf-818af965eee3 creating short code...
INFO: 172.17.0.1:0 - "POST /create_short_code HTTP/1.1" 201 Created
However, when the request comes from the serverside in getserversideprops, like this:
javascript
export async function getServerSideProps({ params }) {
const req = await fetch(`${process.env.API_BASE_URL}/all_short_codes`);
const data = await req.json();
return {
props: { urls: data }
}
}