Report this

What is the reason for this report?

Deployed express server isn't creating a session?

Posted on July 26, 2021

I have deployed a node express server on digital ocean. However, it won’t create the session once it’s deployed. I added a store to prevent memory leak and initialized app.set('trust proxy', 1), before the session. I am creating a user for the session once the user is authenticated. The snippet below shows the configuration for the session.

app.use(
    session({
        key: "userid",
        secret: "subscribe",
        resave: false,
        saveUninitialized: false,
        store: sessionStore,
        cookie: {
            expires: 60000 * 60,
            domain: ".section-webapp-y793v.ondigitalocean.app"
        }
    })
)

app.get("/login", (req, res) => {
    if (req.session.user) {
        res.send({ loggedIn: true, user: req.session.user })
    } else {
        res.send({ loggedIn: false })
    }
}
)


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Heya,

DigitalOcean’s App Platform provides a fully managed environment for deploying applications. However, the issue you’re experiencing seems related to how Express and session handling works in general, rather than anything specific to DigitalOcean’s platform.

In the context of your current issue, you can try the following:

  1. Ensure Secure Cookies: When deploying to a production environment, cookies should be set with the secure: true option to ensure they’re only sent over HTTPS. This is especially true if the server is behind a proxy (which is common on most hosting platforms like DigitalOcean). Try updating the cookie object in your session setup:
cookie: {
    expires: 60000 * 60,
    secure: true,
    httpOnly: true,
    domain: ".section-webapp-y793v.ondigitalocean.app",
    sameSite: "none" // You may or may not need this line depending on your use-case.
}
  1. Set Trust Proxy: When running a server behind a proxy (like what you typically would do on platforms like DigitalOcean), you may need to include the following line before your session setup to ensure the session is correctly maintained:
app.set('trust proxy', 1)
  1. Session Store: Check your session store is working properly in the DigitalOcean environment. If you’re using a memory store like express-session, remember that memory stores are not designed for a production environment. They will leak memory under most conditions, do not scale past a single process, and are meant for debugging and developing.

For a list of compatible session stores, see the compatible session stores in the express-session readme.

Remember, changes to your session or cookie configuration will typically require a server restart to take effect.

If none of these solutions work, please provide more information or any error messages you’re seeing. I’d be happy to help further.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.