Express doesn't set a cookie
<p>I have problem with setting a cookies via express. I'm using <code>Este.js dev stack</code> and I try to set a cookie in API auth <code>/login</code> route. Here is the code that I use in <a href="https://github.com/este/este/blob/master/src/server/api/auth.js" rel="noreferrer"><code>/api/v1/auth/login</code></a> route</p>
<pre><code>res.cookie('token', jwt.token, {expires: new Date(Date.now() + 9999999)});
res.status(200).send({user, token: jwt.token});
</code></pre>
<p>In <code>src/server/main.js</code> I have registered <code>cookie-parser</code> as first middleware</p>
<pre><code>app.use(cookieParser());
</code></pre>
<p>The response header for <code>/api/v1/auth/login</code> route contains</p>
<pre><code>Set-Cookie:token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ.. 
</code></pre>
<p><strong>but the cookie isn't saved in browser</strong> (<code>document.cookie</code> is empty, also <code>Resources - Cookies</code> tab in develepoers tools is empty) :(</p>
<p><strong>EDIT:</strong>
I'm found that when I call this in <code>/api/v1/auth/login</code> (without call <code>res.send</code> or <code>res.json</code>)</p>
<p><code>res.cookie('token', jwt.token, {expires: new Date(Date.now() + 9999999), httpOnly: false});</code>
<code>next();</code></p>
<p><strong>then the cookie is set</strong> AND response header has set <code>X-Powered-By:Este.js</code> ... this sets <code>esteMiddleware</code> in <a href="https://github.com/este/este/blob/master/src/server/frontend/index.js" rel="noreferrer">expres frontend rendering part</a>.</p>
<p>When I use <code>res.send</code></p>
<pre><code>res.cookie('token', jwt.token, {expires: new Date(Date.now() + 9999999), httpOnly: false}).send({user, token: jwt.token});`
next();
</code></pre>
<p>then I get error <code>Can't set headers after they are sent.</code> because <code>send</code> method is used, so frontend render throw this error.</p>
<p>But I have to send a data from API, so how I can deal with this?</p>