如何在下一个js中获取完整的浏览器url,getServerSideProps



现在,我在http://localhost:3000/中,但在生产时,我将在不同的url中,例如http://example.com/,我如何在getServerSideProps中获得完整的浏览器url?我必须获得http://localhost:3000/http://example.com/,如果字符串不包含端口,则可以

在本例中使用的附加答案中:

export const getServerSideProps = async (
context: GetServerSidePropsContext
) => {
const { req } = context;
let url = req.headers.referer;
let arr = url.split('/');
url = `${arr[0]}//${arr[2]}`;

例如,此URLhttp://localhost:3000享受

'next/router'也提供了一些方法,您可以使用这些方法。例如:

import { useRouter } from 'next/router';
const RouterObject = () => {
const { asPath, pathname, req, query, res } = useRouter();
console.log(asPath);
console.log(pathname); 
console.log('host: ', req.headers.host);
}

如果您有主机,您可以检查是否包含localhost。然后你就知道你在当地的环境中。

最新更新