Express会话不保存/查找会话 [英] Express Session not Saving/Finding Session

查看:69
本文介绍了Express会话不保存/查找会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用express-session尝试使用该会话,但似乎找不到或保存该会话.

I am using express-session to try to use the session and it doesn't seem to find or save the session.

我希望每次调用后响应都会增加,但是在Postman或基本的瘦应用程序反复请求的情况下,它始终返回0.

I would expect the response to increment after each call however with repeated requests with Postman or a basic svelte app, it keeps returning 0.

如何获取已保存的会话并返回递增值?

How do i get it to find the already saved session and return incremented values?

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>

推荐答案

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

fetch() by default does not send cookies with its request. So, with no cookies, your server doesn't see the session cookie and then won't find your previous session object. You need to add the credentials: "include" or credentials: "same-origin" option:

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

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

You may also need to call session.save() after changing the session object so your change will persist.

关于您的代码的其他注释:

Some other comments on your code:

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

  1. For most sessions with a real data store behind them, you need to call session.save() after you modify the session object to assure it is saved back to the data store. You probably don't need that for the default memory-based store, but it's a good general practice since production code will probably move away from the memory-based store at some point.

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

Your hardcoding of genid() to always return 1 will break a bunch of features in sessions. Don't do that. The default implementation of genid() will work just fine for your purposes so you can just remove that method definition entirely.

您不会显示用于从服务器加载网页的代码.如果您不是从Web服务器上获取网页本身,则 fetch()调用是一个同源调用,那么您可能还会遇到cors问题(跨原点限制).

You don't show code for loading your web page from your server. If you're not getting the web page itself from your web server so the fetch() call is a same-origin call, then you may also have cors issues (cross origin restrictions).

在客户端代码中,应将变量 resp 的定义移至 sendReq()函数中,以便它是局部变量,而不是如果同一页面上有多个对 sendReq()的调用在进行中,则会被打断.

In your client code, you should move the definition of the variable resp inside your sendReq() function so it is a local variable and would not be trounced if more than one call to sendReq() were ever in flight by the same page.

这篇关于Express会话不保存/查找会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆