简体   繁体   English

Next.js中的getServerSideProps如何添加header

[英]How to add header to getServerSideProps in Next.js

I want to pass JWT access token with a getServersideProps in Next.js. The token is stored in local storage.我想通过 Next.js 中的 getServersideProps 传递 JWT 访问令牌。令牌存储在本地存储中。 Is there any way to do it?有什么办法吗?

Want to add headers to getServerSideProps in Next.js想要在 Next.js 的 getServerSideProps 中添加 headers

getServerSideProps accepts a context parameter which includes keys for accessing the IncomingMessage ( req ) and ServerResponse (res ) objects. getServerSideProps接受一个context参数,该参数包括用于访问IncomingMessage ( req ) 和ServerResponse (res ) 对象的键。 If you want to add a header, you can use the response object's setHeader method.如果要添加一个header,可以使用response对象的setHeader方法。

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

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

  return {
    props: {},
  };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM