0

so I'm making a site with node js, and I need to use localstorage, so I'm using the node-localstorage library. So basically, in one file I add data to it, and in another file I want to retrieve it. I'm not 100% sure about how to retrieve it. I know I need to use localStorage.getItem to retrieve it, but do I need to include localStorage = new LocalStorage('./scratch');? So I was wondering what the localStorage = new LocalStorage('./scratch'); did. So here is my code for adding data:

const ls = require('node-localstorage');
const express = require("express");
const router = express.Router();

router.route("/").post((req, res, next) => {
  var localStorage = new ls.LocalStorage('./scratch');
  if(req.body.name != undefined){
  localStorage.setItem("user", req.body.name);
  res.redirect('/')
  }
  else{
    console.log("undefind")
  }
});

module.exports = router;

If my question is confusing, I just want to know what var localStorage = new ls.LocalStorage('./scratch'); does.

coder
  • 37
  • 7
  • 1
    What are you trying to store? Is the data specific to your server or specific to a user or specific to a request? If it's related to a user or a specific request, you can't store the data in any sort of "globally" accessible store under a common key without creating concurrency problems. The code you show makes it look like you're trying to store one user's name under a common key. That will have concurrency problems. For more detailed help, you should probably describe the higher level problem you're trying to solve so people can suggest more appropriate ways to solve the problem. – jfriend00 Oct 31 '21 at 20:37
  • ok, thanks, I just realized I actually didn't need node-localstorage, because I could set It on the client side. – coder Nov 04 '21 at 01:26

1 Answers1

0

A drop-in substitute for the browser native localStorage API that runs on node.js.

It creates an instance of the "localStorage" class, which this library provides. The constructor expects the location of the file, the scripts stores the key, value elements in.

Opinion: This looks pointless to me - I guess it fits your use case.

madflow
  • 7,718
  • 3
  • 39
  • 54