Express没有设置Cookie [英] Express doesn't set a cookie

查看:261
本文介绍了Express没有设置Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过Express设置Cookie。我正在使用 Este.js开发堆栈,并尝试在API auth / login 路由中设置Cookie。这是我在 / api / v1 / auth / login 路由

I have problem with setting a cookies via express. I'm using Este.js dev stack and I try to set a cookie in API auth /login route. Here is the code that I use in /api/v1/auth/login route

res.cookie('token', jwt.token, {expires: new Date(Date.now() + 9999999)});
res.status(200).send({user, token: jwt.token});

src / server / main.js 我已经将 cookie解析器注册为第一中间件

In src/server/main.js I have registered cookie-parser as first middleware

app.use(cookieParser());

/ api / v1 / auth / login 路线包含

Set-Cookie:token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ.. 

但Cookie并未保存在浏览器中(( document.cookie 为空,develepoers工具中的资源-Cookies 标签为空):(

but the cookie isn't saved in browser (document.cookie is empty, also Resources - Cookies tab in develepoers tools is empty) :(

编辑:
我发现当我在 / api / v1 / auth / login 中调用此命令时(没有调用 res.send res.json

I'm found that when I call this in /api/v1/auth/login (without call res.send or res.json)

res .cookie('token',jwt.token,{expires:new Date(Date.now()+ 9999999),httpOnly:false});
next ();

然后设置cookie ,并且响应标头设置了 X- Powered-By:Este.js ...这会在esteMiddleware /blob/master/src/server/frontend/index.js rel = noreferrer>来回表达渲染渲染部分。

then the cookie is set AND response header has set X-Powered-By:Este.js ... this sets esteMiddleware in expres frontend rendering part.

当我使用 res.send

res.cookie('token', jwt.token, {expires: new Date(Date.now() + 9999999), httpOnly: false}).send({user, token: jwt.token});`
next();

然后我收到错误消息发送标头后无法设置标头。因为使用了 send 方法,所以前端渲染会抛出此错误。

then I get error Can't set headers after they are sent. because send method is used, so frontend render throw this error.

但是我必须从API发送数据,那么如何处理呢?

But I have to send a data from API, so how I can deal with this?

能帮我些忙吗?谢谢!

推荐答案

我遇到了同样的问题。服务器响应带有cookie集:

I had the same issue. The server response comes with cookie set:

Set-Cookie:my_cookie=HelloWorld; Path=/; Expires=Wed, 15 Mar 2017 15:59:59 GMT 

但是cookie并没有被保存浏览器。

But the cookie was not saved by a browser.

这是我解决的方式。

我使用 fetch 在客户端代码中。如果未在 fetch 选项中指定凭据:'include',则cookie既不会发送到服务器,也不会通过

I use fetch in a client-side code. If you do not specify credentials: 'include' in fetch options, cookies are neither sent to server nor saved by a browser, although the server response sets cookies.

示例:

var headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');

return fetch('/your/server_endpoint', {
    method: 'POST',
    mode: 'same-origin',
    redirect: 'follow',
    credentials: 'include', // Don't forget to specify this if you need cookies
    headers: headers,
    body: JSON.stringify({
        first_name: 'John',
        last_name: 'Doe'
    })
})

希望它对某人有帮助。

这篇关于Express没有设置Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆