Search code examples
node.jsreactjsaxiosnavigator

How to access navigator.language in nodejs file?


I want to access navigator.language inside an node js file which is called using axios.get request. I tried to pass the navigator.language from Reactjs file while calling the get request as below:

axios.get('/apps/poe/powerStates', {data: {
        navLanguage : navigator.language
    }});

After going through, some of the links, I thought this would serve my purpose. But no, this didn't pass the navigator.language to the nodejs file where it checks for the navigator.language:

router.get('/example', async (req, res) => {
    console.log(req.data); // shown as blank - {}
    const {navLanguage} = req.data //thus gives me error
    try{
        ........
    } catch{
           ........
    }
  });

Note:- I have seen some external libraries like 'locale' but not looking to use any of the third party libraries for this purpose.

Is there any way for me to achieve this?


Solution

  • You're trying to send data as a body with a GET request - however GET requests do not have a body. That's why your Node.js handler does not see anything inside the body. You should pass your data as URL query parameters:

    // client side request
    axios.get(`/apps/poe/powerStates?lang=${navigator.language}`)
    
    // server side request handler
    router.get('/apps/poe/powerStates', async (req, res) => {
      const { lang } = req.query
      ...
    }