如何在 Next.js 中向 getServerSideProps 添加标头

发布于 2025-01-19 07:24:21 字数 123 浏览 4 评论 0 原文

我想在 Next.js 中使用 getServersideProps 传递 JWT 访问令牌。令牌存储在本地存储中。 有什么办法可以做到吗?

想要向 Next.js 中的 getServerSideProps 添加标头

I want to pass JWT access token with a getServersideProps in Next.js. The token is stored in local storage.
Is there any way to do it?

Want to add headers to getServerSideProps in Next.js

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老街孤人 2025-01-26 07:24:21

getServerSideProps 接受 context 参数,其中包含用于访问 IncomingMessage 的键(req) 和 ServerResponse (res) 对象。如果要添加标头,可以使用响应对象的 setHeader 方法。

export function getServerSideProps(context) {
  // print incoming headers
  console.log(context.req.headers);

  // add header
  context.res.setHeader("X-Foo", "Bar");

  return {
    props: {},
  };
}

getServerSideProps accepts a context parameter which includes keys for accessing the IncomingMessage (req) and ServerResponse (res) objects. If you want to add a header, you can use the response object's setHeader method.

export function getServerSideProps(context) {
  // print incoming headers
  console.log(context.req.headers);

  // add header
  context.res.setHeader("X-Foo", "Bar");

  return {
    props: {},
  };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文