[英]How to use window.sessionstorage in node.js
I store a hash in session storage using javascript like:我使用 javascript 将哈希存储在会话存储中,例如:
window.sessionStorage.setItem('test', true);
How do I read this key using node.js?如何使用 node.js 读取此密钥?
You don't.你没有。
sessionStorage is a browser side API for storing values locally for the life of the browser session, that does not automatically get transmitted to the server. sessionStorage是浏览器端 API,用于在浏览器会话的生命周期内在本地存储值,不会自动传输到服务器。 NodeJS is a framework and engine for creating server side applications. NodeJS 是用于创建服务器端应用程序的框架和引擎。
Perhaps you're needing the functionality of cookies .也许您需要cookie的功能。
I have made a simple package to emulate browser's sessionStorage
.我制作了一个简单的包来模拟浏览器的sessionStorage
。
https://www.npmjs.com/package/node-sessionstorage https://www.npmjs.com/package/node-sessionstorage
You can use it like this:你可以这样使用它:
const storage = require('node-sessionstorage')
storage.setItem('foo', 'bar')
console.log('item set:', storage.getItem('foo'))
I created this npm package since i'm having the same issue on my own projects,我创建了这个 npm 包,因为我在自己的项目中遇到了同样的问题,
npm i sessionstorage-for-nodejs
again this is my npm package and i created it.这又是我的 npm 包,我创建了它。
const sessionStorage = require('sessionstorage-for-node');
sessionStorage.setItem('id', 'value');
console.log('product: ', sessionStorage.getItem('id'));
sessionStorage.removeItem('id', 'value');
You can use express-session
module on Node.js:你可以在 Node.js 上使用express-session
模块:
$ npm install express-session
But it can be more complicated than the way you use window.sessionStorage
, eg some security needs to be taken care of.但它可能比您使用window.sessionStorage
的方式更复杂,例如需要注意一些安全性。 For express-session
documentation, click here .有关express-session
文档,请单击此处。 For more info about how it works, click here .有关其工作原理的更多信息, 请单击此处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.