NextJS-getServerSideProps 中有多个“Set-Cookie”字段?

Duc*_*cHT 20 javascript cookies next.js

在 getServerSideProps 中,我需要在客户端设备上设置多个 cookie。我编码: 在此输入图像描述

Nic*_*ick 31

您可以使用数组作为第二个参数。

ctx.res.setHeader('set-cookie', ['access-token=1', 'refresh-token=1'])
Run Code Online (Sandbox Code Playgroud)

在 Next.js 中,reqres对象只是 Node.js HTTP 对象。因此,您可以在 Node.js 中执行的任何操作都可以使用 Next.js 执行。

https://nodejs.org/api/http.html#http_response_setheader_name_value


小智 9

由于res.setHeaderNextJS 上下文变量中的函数只是一个 NodeJShttp模块 Response 对象,因此您可以为标头设置多个值,如下所示:

res.setHeader('set-cookie', ['val1', 'val2'];
Run Code Online (Sandbox Code Playgroud)

NodeJS 文档中记录了此行为:

此处使用字符串数组来发送具有相同名称的多个标头。