Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!