Why is the GET route in my express server unable to get the express session (with Phusion Passenger)?

25 Views Asked by At

I have been experimenting with my Express servers, and in particular, the session element. I made a quick test using express-session. The POST request works, and responds with the proper value: "amy", however the GET request returns with an empty string, and I have been unable to figure out why. I am also using DreamHost to host my servers, and they use Phusion Passenger to keep my server up and running.

Fearing that my storing methods were improper, I tried using a file based approach, however the same problem occurred - the GET request was still fetching an empty string.

My server and client code are as follows:

test.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Session Test</title>
    <script>
        // Function to handle login
        async function login() {
            try {
                const response = await fetch('https://www.ghestas.com/api/login?name=amy', {
                    method: 'POST',
                    credentials: 'include', // Important for including cookies
                    cache: 'no-store'
                });
                const data = await response.text();
                console.log(data);
                document.getElementById('status').innerText = data;
            } catch (error) {
                console.error('Error:', error);
            }
        }

        // Function to get session data
        async function getSessionData() {
            try {
                const response = await fetch('https://www.ghestas.com/api/loginget', {
                    method: 'GET',
                    credentials: 'include', // Important for including cookies
                    cache: 'no-store'
                });
                const data = await response.text();
                console.log(data);
                document.getElementById('sessionData').innerText = data;
            } catch (error) {
                console.error('Error:', error);
            }
        }
    </script>
</head>
<body>
    <h1>Session Test</h1>
    <button onclick="login()">Login</button>
    <button onclick="getSessionData()">Get Session Data</button>
    <p>Status: <span id="status">Not logged in</span></p>
    <p>Session Data: <span id="sessionData">No data</span></p>
</body>
</html>

app.js (server code):

const express = require('express');
const app = express();
const session = require('express-session')
const store = new session.MemoryStore();

app.use(
    session({
        secret: "secret",
        cookie: { maxAge: 300000000, secure: false, sameSite: "none" },
        saveUninitialized: false,
        resave: false,
        store
    })
);

app.post('/api/login', (req, res, next) => {
    req.session.name = req.query.name;
    res.status(201).send(req.session.name + '- test');
});

app.get('/api/loginget', (req, res, next) => {
    res.send(req.session.name);
});

app.listen(8888, () => {
    console.log(`Server is running!`);
});
0

There are 0 best solutions below