Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

244
Visualizações
Node Express cross origin http request with credentials session not saved

I have one really simple Express back-end with express-session and I want to do some cross origin request. My back-end is hosted on my local : http://localhost:8080 and my front-end is hosted on https://example.com (with SSL). When I made a GET request from https://example.com to http://localhost:8080 the session is not saved, but when I made a request from http://localhost:8081 to http://localhost:8080 (with my front-end deployed locally without SSL) it works

How can I fix this issue with front-end hosted on my domain ?

Back-end:

var express = require('express');
var session = require('express-session');

var app = express();

app.use(function (req, res, next) {
    res.header('Access-Control-Allow-Credentials', true);
    res.header('Access-Control-Allow-Origin', req.headers.origin);
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
    if ('OPTIONS' == req.method) {
        res.send(200);
    } else {
        next();
    }
});

app.use(session({secret: "thisismysecrctekeyfhrgfgrfrty84fwir767", cookie: {httpOnly: true} }));
app.set("trust proxy", 1);

app.disable('etag');


// Status
app.get('/auth', function (req, res) {
    console.log(req.session)
    if(!req.session.key)
    {
        req.session.key = 2;
        req.session.save()
        res.send('<html>COOKIE just saved</html>');
    }
    else
        res.send('<html>COOKIE saved</html>');
});

app.listen(8080, function () {
    console.log('Auth listening on port 8080!')
});

Front-end:

<html>
<script src='https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js'></script>

<script type='text/javascript'>
const instance = axios.create({withCredentials: true});
instance.get('http://localhost:8080/auth').then(function (response) {console.log(response.data);})
</script>

</html>

We can see a difference with the cookie headers, but it's the same front-end code and same back-end, only the host change. A cookie is passed when the front-end is on local, not when it`s hosted with a domain:

Network info hosted front-end on domain : Network info hosted front-end on domain

Network info local front-end : Network info local front-end

I'm using CloudFlare proxy

Thanks a lot!

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda