问题
Typically it seems that when using Express, in the "res" object there is "cookie" so you can do something like:
res.cookie('session', sessionCookie, options);
In the API routes offered by NextJS in Next 9, when looking at res, this does not exist. Is there a way to set the cookie for a response object in a Next API Route function?
回答1:
Adapted from offical repo middleware example, you can set Set-Cookie
header via res
like so:
import { serialize } from 'cookie';
function (req, res) {
// ...
res.setHeader('Set-Cookie', serialize('token', 'token_cookie_value', { path: '/' }));
}
来源:https://stackoverflow.com/questions/57559239/cant-set-a-cookie-using-nextjs-9s-api-route