【问题标题】:Express Session not Saving/Finding Session快速会话不保存/查找会话
【发布时间】:2020-04-07 04:49:14
【问题描述】:

我正在使用 express-session 尝试使用会话,但它似乎没有找到或保存会话。

我希望每次调用后响应都会增加,但是随着 Postman 或基本的苗条应用程序的重复请求,它会一直返回 0。

如何让它找到已经保存的会话并返回递增的值?

Node.js:

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

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "http://localhost:5000"); // update to match the domain you will make the request from
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

app.use(session({
    genid: function(req) {
        return 1
    },
    proxy: true,
    secret: "max",
    resave: false,
    saveUninitialized: true
}));

app.get('/', function (req, res) {
    console.log(req.session)
    if (!req.session.views) {
        req.session.views=0;
    }
    res.send((req.session.views++).toString());
    console.log(req.session)
})

app.listen(3000)

基本苗条:

<script>
    export let name;
    let resp = "";
    async function sendReq() {
        resp = await fetch("http://localhost:3000/");
        console.log(resp);
        resp = await resp.text();
        console.log(resp);
    }
</script>

<main>
    <h1>Hello {name}!</h1>
    <p>Visit the <a href="https://svelte.dev/tutorial">Svelte tutorial</a> to learn how to build Svelte apps.</p>
    <button on:click={sendReq}>Click me</button>
    {resp}
</main>

<style>
    main {
        text-align: center;
        padding: 1em;
        max-width: 240px;
        margin: 0 auto;
    }

    h1 {
        color: #ff3e00;
        text-transform: uppercase;
        font-size: 4em;
        font-weight: 100;
    }

    @media (min-width: 640px) {
        main {
            max-width: none;
        }
    }
</style>

【问题讨论】:

  • 修改会话对象后,应该做session.save()。另外,您为什么要硬编码genId() 以每次都返回相同的ID?除非您出于某些特定原因自己生成会话 ID,否则请删除此方法并让默认行为为您工作。

标签: node.js express session express-session


【解决方案1】:

fetch() 默认情况下不会随请求发送 cookie。因此,如果没有 cookie,您的服务器将看不到会话 cookie,然后将找不到您之前的会话对象。您需要添加credentials: "include"credentials: "same-origin" 选项:

resp = await fetch("http://localhost:3000/", {credentials: "include"});

您可能还需要在更改会话对象后调用session.save(),这样您的更改才会持续。

您的代码中的其他一些 cmets:

  1. 对于大多数在其后具有真实数据存储的会话,您需要在修改会话对象后调用session.save() 以确保将其保存回数据存储。对于默认的基于内存的存储,您可能不需要它,但这是一个很好的通用做法,因为生产代码可能会在某个时候远离基于内存的存储。

  2. genid() 硬编码为始终返回1 会破坏会话中的许多功能。不要那样做。 genid() 的默认实现可以很好地满足您的目的,因此您可以完全删除该方法定义。

  3. 您没有显示用于从服务器加载网页的代码。如果您没有从 Web 服务器获取网页本身,因此 fetch() 调用是同源调用,那么您可能还会遇到 cors 问题(跨源限制)。

  4. 1234563在同一页上飞行。

【讨论】:

  • 是的,这只是一个操场上的东西,可以乱搞和学习快速会话,只是想让它现在工作。当我设置credentials: "same-origin" 时,它不会改变任何东西。但是在设置credentials: "include" 时。我收到一个错误:The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'.
  • 另外,postman 是否默认不保存和重新发送 cookie,或者只是在基本的 svelte 应用程序中进行测试更简单
  • 刚刚发现问题,看起来即使使用内存存储也必须调用 session.save() 。如果您想编辑您的答案,我可以将其标记为正确。此外,将 Allow-Credentials 标头添加为 true。
  • @yodigi7 - 我没有意识到这是一个跨源请求。很高兴你明白了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-06
  • 2019-01-24
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 2019-03-14
相关资源
最近更新 更多