8

I store a hash in session storage using javascript like:

window.sessionStorage.setItem('test', true);

How do I read this key using node.js?

Brown PO
  • 457
  • 3
  • 9
  • 13

4 Answers4

9

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. NodeJS is a framework and engine for creating server side applications.

Perhaps you're needing the functionality of cookies.

Will
  • 2,163
  • 1
  • 22
  • 22
4

I have made a simple package to emulate browser's 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'))
caffeinum
  • 401
  • 5
  • 13
3

I created this npm package since i'm having the same issue on my own projects,

    npm i sessionstorage-for-nodejs

again this is my npm package and i created it.

    const sessionStorage = require('sessionstorage-for-node');

    sessionStorage.setItem('id', 'value');
 
    console.log('product: ', sessionStorage.getItem('id'));
 
    sessionStorage.removeItem('id', 'value');
Ian Ducao
  • 173
  • 9
-1

You can use express-session module on Node.js:

$ npm install express-session

But it can be more complicated than the way you use window.sessionStorage, e.g. some security needs to be taken care of. For express-session documentation, click here. For more info about how it works, click here.

Antonio Ooi
  • 1,601
  • 1
  • 18
  • 32