next js how to get server url in getServerSideProps to fetch data from current api server

Viewed 24

Obviously, this code always returns 404, because of fetch path 'api/registration/' + username, it doesn't work properly with related path .I don't see solution anywhere. Everybody just hardcode link like http://localhost:3000/api/registration/something but why should I hardcode it ? Is there any civilized way to fetch data from api of current server, where actually getServerSideProps was called? (that wouldn't work but anyway window.location.host+"/api/registration/something")

import axios from 'axios';
import { UserData } from '../src/common/types/user-types/UserData';

export default function UsernamePage(props) {
  console.log('Got props', props);
  return <div>Username Page</div>;
}

export async function getServerSideProps({ params: { username }, ...rest }) {
  console.log(rest);
  try {
    const { data } = await axios.get<UserData>('api/registration/' + username);
    return { props: { ...data } };
  } catch (e) {
    return { notFound: true };
  }
}
0 Answers
Related