Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
Is it possible to set localstorage from backend to frontend react js?

I am creating an affiliate functionality on my eCommerce website, I have created link generate functionality, My main concept is if any user buys anything from the generated links the main user who created the URL will get a commission. I cannot be able to store the short generated links in the local storage of a user since when they click on these generated links, they are redirected directly to the targeted page. so I am trying to get the short generated URL from my node js backend. I am using node-localstorage package but it not working.

My expectation is when a user clicks on the generated short link, the generated link will be stored into the local storage anyhow from the frontend or backend.

Please note: the generated link will be shared on many platforms so I want before the user goes to the targeted page the short link will be stored into the local storage because if the user goes to the targeted page the short URL is removed automatically.

This is my frontend code: before executing this useEffect the backend redirects to the targeted page.

useLayoutEffect(() => {
        setLoadings(true)
        fetch(`http://localhost:5000/findUrl/${path}`)
            .then(res => res.json())
            .then(data => {
                if (data.isUrlTrue) {
                    localStorage.setItem('affiliate', path)
                }
            })
            .finally(() => setLoadings(false))
    }, [path])

This is my backend code which is not working.

  app.get('/:shortUrl', async (req, res) => {
            const shortenUrl = await bicycleAffiliateLinksCollection.findOne({ short: req.params.shortUrl })
            if (shortenUrl === null) return res.sendStatus(404)
            // shortenUrl.clicks++
            // shortenUrl.save()

            localStorage.setItem('affliate', "asdasd");
            res.redirect(shortenUrl.full)
        })


about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Store the short URL in the local storage before the browser follows the link:

<a href="short_url" onclick='localStorage.setItem("affiliate","short_url")'>Buy it!</a>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!