"I add user data to req.session.user, and it works locally, but on the Azure server, it doesn't work—no user data is stored. Why? I am using Node.js for the backend."

Artur Shahbazyan 25 Reputation points
2025-03-20T10:11:32.94+00:00

"I add user data to req.session.user, and it works locally, but on the Azure server, it doesn't work—no user data is stored. Why? I am using Node.js for the backend."

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,689 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Alekhya Vaddepally 820 Reputation points Microsoft External Staff
    2025-03-20T13:49:57.9333333+00:00

    Hi Artur Shahbazyan,

    The problem wherein req.session.user is working perfectly locally but not on the Azure server should probably have to do with session storage. By default, in a local run, sessions are stored in memory, which is great for development. But in production or on Azure App Service, the app tends to be run in a load-balanced or scaled scenario where several instances of the app are created.

    In these cases:

    In-memory session storage is lost upon restarting or scaling the instance.

    All data held in memory, including session data, is lost, so req.session.user is not carried forward on Azure.

    You can also install necessary packages

    Use a persistent session store or setup Redis in Azure such as Redis, MongoDB, or Azure Table Storage. For instance, with Redis example:

    javascript

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

    const RedisStore = require('connect-redis')(session);

    app.use(session({

    store: new RedisStore({ host: 'your-redis-host', port: 6379 }),

    secret: 'your-secret-key',

    resave: false,

    saveUninitialized: false,

    }));

    Also set secure: true only if using HTTP, Use cookie.maxAge to control session expiration.
    https://learn.microsoft.com/en-us/azure/azure-cache-for-redis/cache-nodejs-get-started?tabs=entraid&pivots=azure-managed-redis
    https://learn.microsoft.com/en-us/azure/app-service/tutorial-nodejs-mongodb-app?tabs=copilot&pivots=azure-portal
    If the answer is helpful, please click Accept Answer and kindly upvote it so that other people who faces similar issue may get benefitted from it.

    Let me know if you have any further Queries.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.