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

209
Visualizações
express-session not being saved locally but works in production

So I am running a node.js backend with a react frontend, and for some reason with this setup:

app.use(session({
    store: new MongoStore({
        mongooseConnection: mongoose.connection
    }),
    secret: process.env.SECRET_KEY,
    resave: false,
    saveUninitialized: false,
    cookie: {
        maxAge: 1000 * 60 * 60 * 24 * 7 * 2, // two weeks
        secure: false,
        httpOnly: false,
    }
}));

I save the username using: req.session.username = username; which does save but then whenever I try to check the session on another page the username is gone.

Full source code: https://github.com/CTF-Cafe/CTF_Cafe/tree/master/backEnd

PS: it works in production completely fine, but not locally I tried with httponly false, and secure false. No change.

Any help appriciated

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

When you serve your frontend from your backend the session will be working because it has the same origin. However, for your local environment, you will be running React app in a different port, and the Express app would be also different. When a port is different it is already considered a different origin and cookies, sessions won't be shared.

You can proxy your frontend via the node app and this way you can have the same origin and your session will remain.

const proxy = require('express-http-proxy');
const app = express();

...
// Add after your routes
app.use(proxy('http://127.0.0.1:3000')); // assuming your Reacts runs on P3000

Check this answer too.

over 4 years ago · Santiago Trujillo Relatório
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