'How to access localStorage in node.js?

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?



Solution 1:[1]

You can use :

node-localstorage npm module to use localStorage at the server side.

var LocalStorage = require('node-localstorage').LocalStorage,
localStorage = new LocalStorage('./scratch');

Solution 2:[2]

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

Solution 3:[3]

When the page loads, send a post that queries the contents of the client's localStorage.

Solution 4:[4]

Found this store

// Store current user 
store.set('user', { name:'Marcus' })

// Get current user 
store.get('user')

// Remove current user 
store.remove('user')

// Clear all keys 
store.clearAll()

// Loop over all stored values 
store.each(function(value, key) {
    console.log(key, '==', value)
})

Solution 5:[5]

For Node.js you can use HandyStorage, a fast and small npm package which behaves data like a state Here's an example:

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!

Solution 6:[6]

LocalStorage is never accessible by the server. Ever. It would be a huge security issue.

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.

Cookies work well for when you need to pass small amounts of data regularly between server and client.

Databases on your server are best if you need to store data long-term.

Solution 7:[7]

In the JS file you write this:

const LocalStorage = require('node-localstorage').LocalStorage,
  localStorage = new LocalStorage('./scratch');

Also you need: open new Terminal press Control + C (don't worry it's for clear not for copy) and type you'r file.js (for ex: server.js) and than type this: npm i node-localstorage (now you all set).

Solution 8:[8]

Well, I think some explanations are in handy, first of all, node JS and V8 are two different things and runs on different places. Browsers uses the v8 engine to run JS, node JS runs on servers and is v8 based, but it doesn't have everything that the browser does (like the window object). So you can't access the browser local storage from your server because it's running somewhere (browsers runs on the user machine) else, I think that you want a lib that works like local storage but in your node js serve, for that we have store -> https://www.npmjs.com/package/store.