如何在 node.js 中访问 localStorage?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10358100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access localStorage in node.js?
提问by Chris Abrams
I tried searching the web for a node module that can access the client's localStorage but wasn't able to find anything. Anyone know of one?
我尝试在网络上搜索可以访问客户端 localStorage 的节点模块,但找不到任何内容。有人知道吗?
回答by VijeshJain
回答by Gus
If you mean html 5 localStorage, there's no such a thing since node.js is a server-side technology. Html 5 localStorage is a client side feature supported
如果您的意思是 html 5 localStorage,则没有这样的事情,因为 node.js 是一种服务器端技术。Html 5 localStorage 是支持的客户端功能
回答by Larry Battle
When the page loads, send a post that queries the contents of the client's localStorage.
当页面加载时,发送查询客户端 localStorage 内容的帖子。
回答by msmaromi
回答by floodlitworld
LocalStorage is never accessible by the server. Ever. It would be a huge security issue.
服务器永远无法访问 LocalStorage。曾经。这将是一个巨大的安全问题。
If you need to send it to a server, then you have to have a client-side JS script which retrieves it, and then sends it to the server as part of an Ajax or POST request.
如果您需要将它发送到服务器,那么您必须有一个客户端 JS 脚本来检索它,然后将它作为 Ajax 或 POST 请求的一部分发送到服务器。
Cookies work well for when you need to pass small amounts of data regularly between server and client.
当您需要在服务器和客户端之间定期传递少量数据时,Cookie 非常有用。
Databases on your server are best if you need to store data long-term.
如果您需要长期存储数据,则服务器上的数据库是最佳选择。
回答by balwant mahara
We can not save to localStorage on Node.js.
我们无法在 Node.js 上保存到 localStorage。
回答by Alireza Sheikholmolouki
For Node.js you can use HandyStorage, a fast and small npm package which behaves data like a stateHere's an example:
对于 Node.js,您可以使用HandyStorage,这是一个快速且小型的 npm 包,其行为类似于state以下示例:
const HandyStorage = require('handy-storage');
const storage = new HandyStorage('./store.json');
storage.setState({
name: 'Alireza',
lastname: 'Sh',
friends: [
'Jane',
'John'
],
visited: storage.state.visited || 0
})
storage.setState({
visited: storage.state.visited + 1
})
It automatically changes the JSON file, just give it a try!
它会自动更改 JSON 文件,试试看吧!

