Express Session 不是 Saving/Finding Session
Express Session not Saving/Finding Session
我正在使用 express-session 尝试使用会话,但它似乎没有找到或保存会话。
我希望每次调用后响应都会增加,但是对于 Postman 或基本的 svelte 应用程序的重复请求,它会保持 returning 0。
如何找到已保存的会话和 return 增量值?
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,也就找不到您之前的会话对象。您需要添加 credentials: "include"
或 credentials: "same-origin"
选项:
resp = await fetch("http://localhost:3000/", {credentials: "include"});
您可能还需要在更改会话对象后调用 session.save()
,这样您的更改才会保留。
对您的代码的一些其他评论:
对于大多数背后有真实数据存储的会话,您需要在修改会话对象后调用 session.save()
以确保将其保存回数据存储。对于默认的基于内存的存储,您可能不需要它,但这是一个很好的通用做法,因为生产代码可能会在某个时候离开基于内存的存储。
您将 genid()
硬编码为始终 return 1
会破坏会话中的许多功能。不要那样做。 genid()
的默认实现将很好地满足您的目的,因此您可以完全删除该方法定义。
您没有显示从服务器加载网页的代码。如果您不是从您的 Web 服务器获取网页本身,所以 fetch()
调用是同源调用,那么您可能还会遇到 cors 问题(跨源限制)。
在您的客户端代码中,您应该将变量 resp
的定义移到 sendReq()
函数中,这样它就是一个局部变量,如果超过同一页面曾经对 sendReq()
进行过一次调用。
我正在使用 express-session 尝试使用会话,但它似乎没有找到或保存会话。
我希望每次调用后响应都会增加,但是对于 Postman 或基本的 svelte 应用程序的重复请求,它会保持 returning 0。
如何找到已保存的会话和 return 增量值?
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,也就找不到您之前的会话对象。您需要添加 credentials: "include"
或 credentials: "same-origin"
选项:
resp = await fetch("http://localhost:3000/", {credentials: "include"});
您可能还需要在更改会话对象后调用 session.save()
,这样您的更改才会保留。
对您的代码的一些其他评论:
对于大多数背后有真实数据存储的会话,您需要在修改会话对象后调用
session.save()
以确保将其保存回数据存储。对于默认的基于内存的存储,您可能不需要它,但这是一个很好的通用做法,因为生产代码可能会在某个时候离开基于内存的存储。您将
genid()
硬编码为始终 return1
会破坏会话中的许多功能。不要那样做。genid()
的默认实现将很好地满足您的目的,因此您可以完全删除该方法定义。您没有显示从服务器加载网页的代码。如果您不是从您的 Web 服务器获取网页本身,所以
fetch()
调用是同源调用,那么您可能还会遇到 cors 问题(跨源限制)。在您的客户端代码中,您应该将变量
resp
的定义移到sendReq()
函数中,这样它就是一个局部变量,如果超过同一页面曾经对sendReq()
进行过一次调用。